Android media player sound is compressed / stutters only on newer devices

I have a pretty standard media glider object that runs in onCreate and loops for background music in my application. The file is not unusually large, it's 6 MB MP3. From onCreate:

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

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

Unfortunately, I experience problems exclusively with newer devices, where I encounter a sound / stutter failure on the Nexus 5 and the new Nexus 10. These problems ONLY happen on new devices, usually after a few seconds of correct playback, and not immediately. Both of my Nexus 4 and 5 are running Android 4.4.4, but the problem remains only on the Nexus 5.

These problems seem to be exacerbated when I pause the media glider object and play for a short period of time (combat music for short fights in the game), but the crash occurs even without this additional complication.

I read that new versions of Android caused problems with Mediaplayer, but I did not come up with a fix or suggestion.

Has anyone else experienced this problem, who can suggest a fix or workaround? Thank you for your time!

+6
source share
1 answer

I noticed that this is happening on my Android devices as well.

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

As for your problem when switching between sources, I suggest calling SeekTo () the exact time in the audio that you want to play, and wait for this position using the SeekComplete listener. In the comments, I have a small line of hacker code that does not install the volume on the media player before the call. This seems to reduce stuttering, but you may lose the first small part of the sound.

I use Xamarin Studio C #, but even if you use Java, the 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); 
0
source

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


All Articles