1

I have an issue with Media player always stopping randomly after some few mins, most times, when the phone sleeps and you touch it again, the video hangs and stops playing.

I have tried many solutions here on stackoverflow, none worked. I'm almost pulling my hair out !!!

I've tried this answer

Android MediaPlayer stops playing after some time

MediaPlayer randomly stops on Android 4.4 (19)

How to prevent mediaplayer to stop when screen goes off?

and many more answers, they all didn't work

This is my code below

public class MainActivity {

    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

        mp = MediaPlayer.create(this, R.raw.my_webm_video);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        playVideo();

    }

    @Override
    public void onResume() {
        super.onResume();
        mp = MediaPlayer.create(this, R.raw.my_webm_video);
        mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        playVideo();
    }

    public void playVideo(){

        SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
        SurfaceHolder holder = sv.getHolder();
        holder.addCallback(new SurfaceHolder.Callback(){
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
               mp.setDisplay(holder);
               mp.start();
               mp.setLooping(true);
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }

        });
    }


}

This is my XML layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
Community
  • 1
  • 1
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61

1 Answers1

0

ok, so i finally solved the problem. Incase any one else run into this issue

in onCreate i checked if the video was already playing, to make sure it isn't called to re-play each time.

int firstPlayed = 0;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mp = MediaPlayer.create(this, R.raw.my_webm_video);
        mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        if(!mp.isPlaying()) {
            firstPlayed = 1;
            playVideo();
        }
}

I checked in onResume incase the user navigates out of the activity and goes back, Which makes the video screen turn black.

public void onResume() {
        super.onResume();
         if(!mp.isPlaying() && firstPlayed == 0) {
            mp = MediaPlayer.create(this, R.raw.my_webm_video);
            mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            playVideo();
        }
        firstPlayed = 0;
}

And then i noticed that i needed a firstPlayed check to be sure.

Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61