开发者

Android MediaPlayer issue

开发者 https://www.devze.com 2023-01-10 15:34 出处:网络
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:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号