MediaPlayer won't loop. setLooping() doesn't work

后端 未结 4 1083
花落未央
花落未央 2021-01-18 05:54
m_MediaPlayer = MediaPlayer.create(context, soundFromResource);
m_MediaPlayer.setVolume(0.99f, 0.99f);
m_MediaPlayer.setLooping(true);
m_MediaPlayer.setOnCompletionL         


        
相关标签:
4条回答
  • 2021-01-18 06:05

    I solve this using this workaround:

    public static void setMpLooping(final MediaPlayer mp, final boolean isLooping) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
                || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (isLooping) {
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.start();
                    }
                });
            } else {
                mp.setOnCompletionListener(null);
            }
        } else {
            mp.setLooping(isLooping);
        }
    }
    

    But remember, sometimes there is a delay during the transition from end to start of the track on Lollipop

    0 讨论(0)
  • 2021-01-18 06:07

    After a lot of experimentation with media player, I have found solution to this :

    call

    m_MediaPlayer.setLoopoing(true)
    

    after

    m_MediaPlayer.start()
    
    0 讨论(0)
  • 2021-01-18 06:23

    It is an issue in media player.

    I used the next code to fix that:

    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        if (looping) {
                            mMediaPlayer.seekTo(0);
                        }
                    }
                });
    
    0 讨论(0)
  • 2021-01-18 06:24

    Apparently there's an issue with Android 5 devices which use the NuPlayer instead of the AwesomePlayer.

    You can check it by going in the Developer Options, under the Media section there should be Use NuPlayer (experimental). I've unchecked that and it appears it's alright now.

    I haven't been able to figure out how to fix this issue, so I've hacked it a bit. I've set some flags in the code and when it enters onCompletion, if the user hasn't specifically stopped the sound, i restart it there. If there's anyone with a better fix, let me know and i'll update this answer.

    Here's the issue: https://code.google.com/p/android-developer-preview/issues/detail?id=1695

    0 讨论(0)
提交回复
热议问题