Sanku
Sanku

Reputation: 511

Android Studio- Media Player doesn't play sound On Genymotion?

I am trying to build a small app which plays a sound when we click on the button. But I am not able to play the sound. Don't know what the problem is. Please help me on this. Below is the code.

 public class MainActivity extends AppCompatActivity {

    private Button button;
    private MediaPlayer mediaPlayer;

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

        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);


        button = (Button)findViewById(R.id.mediaButtonId);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              mediaPlayer.start();

            }
        });
    }

}

Note:-Sorry guys,I thought that the problem is with my code but the app is running perfectly fine on my phone,so its the problem with my genymotion emulator.Can anyone please suggest me the solution for this.By the way,I am using Mac OSX.

Upvotes: 0

Views: 1980

Answers (2)

MichaelStoddart
MichaelStoddart

Reputation: 5639

You need to make sure the media player is ready before you can play it, so you set the onPreparedListener to handle this for you, like so:

MediaPlayer mp = new MediaPlayer();

mp = MediaPlayer.create(getApplicationContext(),R.raw.song);

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mp.start();
    }
});

button = (Button)findViewById(R.id.mediaButtonId);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      mp.prepareAsync();
    }
});

There will be a slight delay from when you press the button to the sound playing this way. Another way to do it could be to disable the button until the media player has prepared and then in the onclick of the button you could just call mp.start(); when the button has been enabled.

Upvotes: 1

Rui Santos
Rui Santos

Reputation: 547

The MediaPlayer has its own lifecycle. You can't just create the instance and then start to play. First you have to prepare it and then play it.

You can prepare your mediaplayer either sync or asynchronously.

Something along the lines of:

MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
   mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();

Or, if you want to do it synchronously

    MediaPlayer mediaPlayer= new MediaPlayer();
    mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
    try {
        mediaPlayer.prepare();
    } catch (IOException e){

    }
    mediaPlayer.start();

Just make sure you prepare it before you play it.

Media Player lifecycle: https://developer.android.com/reference/android/media/MediaPlayer.html

Upvotes: 1

Related Questions