MediaPlayer will not loop. setLooping () does not work

m_MediaPlayer = MediaPlayer.create(context, soundFromResource); m_MediaPlayer.setVolume(0.99f, 0.99f); m_MediaPlayer.setLooping(true); m_MediaPlayer.setOnCompletionListener(USoundPlayback.this); m_MediaPlayer.start(); 

I tested it as above, I also tested it by calling setLooping(true) after start() , but no luck.

I have two Nexus 5 phones, both with Android 5 on them. On one, the cycle works, on the other hand, the sound stops after one time, it will not loop.

Any ideas ?!

+5
source share
3 answers

There seems to be a problem with Android 5 devices that use NuPlayer instead of AwesomePlayer .

You can check it by going to Developer Options , in the Media section should be Use NuPlayer (experimental) . I unchecked the box, and now everything is in order.

I was not able to figure out how to fix this problem, so I hacked it a bit. I set several flags in the code and when it enters onCompletion , if the user hasn’t specially stopped the sound, I will restart it there. If anyone with a better fix let me know and I will update this answer.

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

+5
source

This is a problem in the media player.

I used the following code to fix this:

 mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if (looping) { mMediaPlayer.seekTo(0); } } }); 
+1
source

I solve this with 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 the end to the beginning of the track on Lollipop

0
source

Source: https://habr.com/ru/post/1213541/


All Articles