Tutorial :Android MediaPlayer issue



Question:

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


Solution:1

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();  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »