Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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've tested it like above, I've also tested it by calling setLooping(true) after start() but with no luck.

I have two Nexus 5 phones, both with Android 5 on them. On one, the looping works, on the other the sound stops after one go, it won't loop.

Any ideas ?!

like image 625
AndreiBogdan Avatar asked Feb 17 '15 16:02

AndreiBogdan


3 Answers

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

like image 142
AndreiBogdan Avatar answered Nov 08 '22 08:11

AndreiBogdan


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

call

m_MediaPlayer.setLoopoing(true)

after

m_MediaPlayer.start()
like image 43
Abhishek Luthra Avatar answered Nov 08 '22 08:11

Abhishek Luthra


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);
                    }
                }
            });
like image 20
Mahmoud Avatar answered Nov 08 '22 07:11

Mahmoud