Darius
Darius

Reputation: 5269

Android MediaPlayer issue

I have followed the steps to create my media player object but I cant understand why it is not playing the music track. I used the following code:

mp = new MediaPlayer();
    mp.create(this, R.raw.testmed); 
    mp.setVolume(100, 100);


    mp.start();

but no sound is playing through the emulator, and furthermore when i check the method mp.isPlaying() it returns false. What have I missed?!

Many thanks

Upvotes: 1

Views: 8129

Answers (1)

slhck
slhck

Reputation: 38750

You have to call all methods necessary to actually start the player. Take a look at Android Media Player state diagram

I think you need to prepare the player before starting. When the player is prepared, it can be started. This is done through a onPreparedListener:

mp = new MediaPlayer();
mp.create(this, R.raw.testmed);
mp.setVolume(100, 100);
mp.setOnPreparedListener(this);
mp.prepare();

Then you will need to define this and it should work:

public void onPrepared(MediaPlayer player) {
   mp.start();
}

Upvotes: 4

Related Questions