سؤال

I create a simple project with one layout that contains two buttons, and this is my code:

package com.example.tessound;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener
{
    MediaPlayer player;
    Button play,mute;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play = (Button)findViewById(R.id.button1);
        play.setOnClickListener(this);
        mute = (Button)findViewById(R.id.button2);
        mute.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View view)
    {
        if(view.getId()==R.id.button1)
        {
            playSound(1);
        }
        else if(view.getId()==R.id.button2)
        {
            playSound(2);
        }
    }

    public void playSound(int arg)
    {
        if (arg == 1)
        {
            player = MediaPlayer.create(this, R.raw.atur);
        }
        else if (arg == 2)
        {
            player = MediaPlayer.create(this, R.raw.back);
        }

        if(player != null)
        {
            player.setLooping(false);
            player.start();
        }

        try
        {
            if(player != null)
            {
                if (player.isPlaying()) 
                {
                    player.stop();
                    player.release();
                }
            }
        }
        catch(Exception e)
        {
        }
    }
}

When I tried to click the button the sound doesn't play.

هل كانت مفيدة؟

المحلول

Following the logic for your playSound method with an argument value of 1:

1) arg == 1 so:

player = MediaPlayer.create(this, R.raw.atur);

2) player has been set so is not null, hence:

player.setLooping(false);
player.start();

3) Then it's your try block. player is not null and is playing, hence:

player.stop();
player.release();

So I think you are starting the playback and then immediately stopping it. I imagine you should only execute the try/catch code if the method does not receive a valid argument, i.e. it should be an 'else' of the preceding 'if' statement.

EDIT:

Looking at this again, I think the try/catch code should go at the top of the method. It will then stop the player and release it (if it is in use) before trying to start playing a new sound. Logically that makes sense.

نصائح أخرى

Use Log to trace the control and find the error !

Log.e("AnyTAG","Description of the Log");

your code seems fine!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top