几分钟后android media player停止播放 [英] android media player stops playing after some minutes

查看:205
本文介绍了几分钟后android media player停止播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,媒体播放器总是在几分钟后(大多数时候)随机休眠,大多数情况下,当手机进入睡眠状态并再次触摸时,视频会挂起并停止播放.

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.

我在这里尝试了许多关于stackoverflow的解决方案,但都没有成功.我几乎要拔头发了!!!

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

我已经尝试过这个答案

Android MediaPlayer在一段时间后停止播放

MediaPlayer在Android 4.4(19)上随机停止

如何防止媒体播放器在屏幕进入时停止播放关闭?

还有更多答案,它们都没有作用

and many more answers, they all didn't work

这是我的下面的代码

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) {

            }

        });
    }


}

这是我的XML布局

<?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>

推荐答案

好的,所以我终于解决了这个问题.万一其他人遇到这个问题

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

在onCreate中,我检查了视频是否已经在播放,以确保每次都不被调用来重新播放视频.

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();
        }
}

我签入了onResume,以防用户从活动中退出并返回,这会使视频屏幕变黑.

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;
}

然后我注意到我需要进行一次firstPlayed检查才能确定.

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

这篇关于几分钟后android media player停止播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆