MediaPlayer is not looping

僤鯓⒐⒋嵵緔 提交于 2019-12-10 15:48:41

问题


I want to create activity background music, but MediaPlayer playing not repeatedly :( Why my MediaPlayer is not looping?

There is my code:

MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.tersetetete);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setVolume(8f, 8f);
    mediaPlayer.setLooping(true);

    mediaPlayer = MediaPlayer.create(this, R.raw.fon);
    mediaPlayer.start();
}

回答1:


Replace mediaPlayer = new MediaPlayer(); with the line mediaPlayer = MediaPlayer.create(this, R.raw.fon); that you wrote below.

You are having the issues because new MediaPlayer(); creates a new MediaPlayer object on which you set Volume and Looping properties, but after that you're creating a new object with MediaPlayer.create(this, R.raw.fon); and then you play the sound represented by that new object which doesn't have any looping property set to true nor any volume of 8f 8f.

Here's the full code you can use:

MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.tersetetete);

    mediaPlayer = MediaPlayer.create(this, R.raw.fon);
    mediaPlayer.setVolume(.8f, .8f);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
}



回答2:


call

 mediaPlayer.setLooping(true);

after

    mediaPlayer.start(); 



回答3:


I am on KITKAT and setlooping(true) was not working for me, so I made this changes and now it works...

                MediaPlayer mMediaPlayer;
                //Stop on going Music
                AudioManager audiomanager = (AudioManager)context.getSystemService("audio");
                if (audiomanager.isMusicActive())
                {
                    Intent intent = new Intent("com.android.music.musicservicecommand");
                    intent.putExtra("command", "pause");
                    context.sendBroadcast(intent);
                }
                //Setting Max Volume
                audiomanager.setStreamVolume(3, audiomanager.getStreamMaxVolume(3), 0);
                mMediaPlayer = MediaPlayer.create(context.getApplicationContext(),R.raw.file);
                mMediaPlayer.start();
                //This did the trick
                mMediaPlayer.setOnErrorListener(new android.media.MediaPlayer.OnErrorListener() {

                    public boolean onError(MediaPlayer mediaplayer, int i, int j)
                    {
                        return false;
                    }
                });
                mMediaPlayer.setLooping(true);

And to stop I have used.

               mMediaPlayer.release();


来源:https://stackoverflow.com/questions/16515455/mediaplayer-is-not-looping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!