Android mediaplayer audio glitches/stutters only on newer devices
Asked Answered
R

2

7

I have a fairly standard mediaplayer object that starts up in onCreate and loops for background music in my app. The file isn't unusually large, it's a 6MB MP3. From the onCreate:

MediaPlayer mp;
mp = MediaPlayer.create(MainActivity.this, R.raw.lostmexicancity);
mp.setLooping(true);
mp.setVolume(0.4f, 0.4f);
mp.start();

This works just fine on most of my test devices including older phones, a Samsung Galaxy Tab 2 10" tablet, and even a Nexus 4.

Unfortunately, I am experiencing problems exclusively with newer devices where I encounter audio glitching/stuttering on the Nexus 5 and the newer Nexus 10. These issues ONLY happen on newer devices, usually after a few seconds of proper playback, not immediately. Both my Nexus 4 and 5 are running Android 4.4.4 and yet the issue only happens on the Nexus 5.

This issues seems to be exacerbated when I pause that mediaplayer object and play a different one for a short period of time (battle music for short fights in the game) but the glitching happens even without this additional complication.

I have read that newer versions of Android have caused issues with Mediaplayer, but I haven't come upon a fix or suggestion.

Has anyone else experienced this issue who can suggest a fix or work-around? Thank you for your time!

Routh answered 19/1, 2015 at 21:19 Comment(1)
Did you get any solution for this problem?Victualer
P
0

I have noticed this happening on my Android devices as well.

I notice that you are not calling Prepare() which is an important function before playing audio. EDIT-- a call to prepare is only necessary when creating MediaPlayer using new, not with the built-in MediaPlayer.Create().

As for your issue when switching between sources, I suggest making a call to SeekTo() the exact time in the audio that you want to play and wait for that position with the SeekComplete listener. In comments, I have small line of hacky code that does not set the volume on the mediaPlayer until after the call to start. This seems to reduce the stutter, but you may lose the first small fraction of audio.

I am using Xamarin Studio C#, but even if you are using Java, this same approach should work.

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.SetAudioStreamType (Android.Media.Stream.Music);
mediaPlayer.SetDataSource ("dataSourcePath");

mediaPlayer.Looping = true;

//It is necessary to call prepare after setting the data source
mediaPlayer.Prepare ();

//Ensure the audio has seeked to the position you need
bool seekingComplete = false;
mediaPlayer.SeekComplete += (object sender, EventArgs e) => {
    seekingComplete = true;
};

mediaPlayer.SeekTo(0);

//Forces the audio to complete seeking
while(seekingComplete == false)
{
    //Here, you just wait 2 milliseconds at a time 
    //for this buffering and seeking to complete
    await Task.Delay(2);
}

mediaPlayer.Start();

//Hacky way to prevent the glitch sound at the start is to set the
//volume after calling start
//mediaPlayer.SetVolume(0.4f, 0.4f);
Powered answered 26/5, 2015 at 21:45 Comment(1)
Calling MediaPlayer.create() automatically prepares the audio. You definitely should not be calling it again. From the docs: "Convenience method to create a MediaPlayer for a given Uri. On success, prepare() will already have been called and must not be called again."Insincere
E
0

I had a similar problem, and resolved it by always pausing the media player before seeking. Something like the following worked for me:

if (mediaPlayer.isPlaying()) {
    mediaPlayer.pause();
    mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
        @Override
        public void onSeekComplete(MediaPlayer mp) {
            mp.start();
            mp.setOnSeekCompleteListener(null);
        }
    });
}
mediaPlayer.seekTo(pos);
Earmark answered 10/10, 2018 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.