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

后端 未结 4 1085
花落未央
花落未央 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

提交回复
热议问题