MediaPlayer freezes instead of looping video on Android 4.4

I have a problem playing video on Android 4.4 on the Galaxy 3 tab. The problem did not occur with the previous version of Android 4.2.

Problem

I play a video in a loop. This worked very well for all users until Tab 3 upgraded to Android 4.4. Since then, the video freezes after the first cycle (it gets stuck on the first frame of the video to be exact).

I can reproduce this behavior, and the moment the video freezes, my LogCat starts to fill up with the following output:

16:25:25.239 14589-14601/my.app V/MediaPlayer? back from callback 16:25:25.499 14589-14686/my.app V/MediaPlayer? message received msg=7, ext1=0, ext2=0 16:25:25.499 14589-14686/my.app V/MediaPlayer? unrecognized message: (7, 0, 0) 16:25:25.499 14589-14686/my.app V/MediaPlayer? callback application 16:25:25.499 14589-14686/my.app V/MediaPlayer? back from callback 16:25:25.519 14589-14602/my.app V/MediaPlayer? message received msg=4, ext1=0, ext2=0 16:25:25.519 14589-14602/my.app V/MediaPlayer? Received seek complete 16:25:25.519 14589-14602/my.app V/MediaPlayer? All seeks complete - return to regularly scheduled program 16:25:25.519 14589-14602/my.app V/MediaPlayer? callback application 16:25:25.519 14589-14602/my.app V/MediaPlayer? back from callback 16:25:25.519 14589-14601/my.app V/MediaPlayer? message received msg=6, ext1=0, ext2=0 16:25:25.519 14589-14601/my.app V/MediaPlayer? Received MEDIA_STARTED 16:25:25.519 14589-14601/my.app V/MediaPlayer? callback application 16:25:25.519 14589-14601/my.app V/MediaPlayer? back from callback 16:25:25.789 14589-14686/my.app V/MediaPlayer? message received msg=7, ext1=0, ext2=0 16:25:25.789 14589-14686/my.app V/MediaPlayer? unrecognized message: (7, 0, 0) 16:25:25.789 14589-14686/my.app V/MediaPlayer? callback application 16:25:25.789 14589-14686/my.app V/MediaPlayer? back from callback 16:25:25.809 14589-14602/my.app V/MediaPlayer? message received msg=4, ext1=0, ext2=0 16:25:25.809 14589-14602/my.app V/MediaPlayer? Received seek complete 

Code

It is assumed that the following video program (simple) will be played. MediaPlayer.OnErrorListener () and MediaPlayer.OnInfoListener () are never called.

 public class VideoActivity extends Activity { private MediaPlayer mediaPlayer; private String videoPath = "some path obtained from the system"; // [...] @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mediaPlayer = new MediaPlayer(); startMovie(); } private void startMovie(){ mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.setLooping(true); mediaPlayer.setDataSource(videoPath); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); mp.start(); mp.seekTo(0); } }); mediaPlayer.prepare(); } } 

The video can be launched several times using a different path to the video file throughout the life cycle, so I stop and reset the player before the movie starts.

So far, the only solutions I have found on the Internet have been to make sure MediaPlayer is not going to GC (which I do without making it a local method) and implement WakeLock, which I also made without effect.

Can someone help me here and point me in the right direction? Thanks!

+2
source share
1 answer

More workaround than solution, in the end I did not loop the video using the setLooping MediaPlayer method. Instead, I now restart the video in the OnCompletion MediaPlayer OnCompletion . After many attempts, this was the only way to get me to work on all the devices I am testing on.

 public class VideoActivity extends Activity { private MediaPlayer mediaPlayer; private String videoPath = "some path obtained from the system"; // [...] @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mediaPlayer = new MediaPlayer(); startMovie(); } private void startMovie(){ mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.setDataSource(videoPath); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); mp.start(); mp.seekTo(0); } }); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { startMovie(); } }); mediaPlayer.prepare(); } } 

However, if someone has the correct solution to this problem, I will be happy to mark the corresponding answer as the correct one.

+3
source

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


All Articles