android media player stops playing after some minutes

纵饮孤独 提交于 2019-12-23 01:52:26

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/37343910/android-media-player-stops-playing-after-some-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!