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
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();
}
精彩评论