当返回到视频播放活动的Andr​​oid黑屏 [英] Black screen when returning to video playback activity in Android

查看:963
本文介绍了当返回到视频播放活动的Andr​​oid黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Android应用ServeStream,我已经遇到的问题,我无法修复。我的应用程序将使用机器人的MediaPlayer类的流音乐和视频。我在这里找到的例子模仿我的类:

I'm currently developing the android application ServeStream and I've encountered and problem that I can't fix. My application will stream music and video using the android MediaPlayer class. I've modeled my class after the example found at:

<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

这个例子和我自己的code之间的区别是我的MediaPlayer正在运行的服务,使其能够继续播放的背景。与例如机器人code的问题是,如果我在看一个视频,我离开当前窗口/活动(即preSS菜单按钮等),并返回播放活动我得到一个黑色的屏幕,但仍然正在播放的视频接收音频。

The difference between this example and my own code is my MediaPlayer is runs in a service which allows it to continue playback in the background. The problem with the example android code is if I'm watching a video and I leave the current window/activity (i.e press the menu button, etc) and return to the playback activity I get a black screen but still receive audio from the video that is playing.

当我播放活动最初创建执行如下所示的code。这code实质上创建播放所使用的视图,然后它关系到媒体播放器:

When my playback activity is initially created the code shown below is executed. This code essentially creates the view used for playback and then ties it to the media player:

        setContentView(R.layout.mediaplayer_2);
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
...
        mMediaPlayer.setDisplay(holder);

的重要线是mMediaPlayer.setDisplay(保持器),因为它关系当前视图/显示到媒体播放器。该视图(持有人),当您离开活动被破坏。返回到活性和重新创建视图,执行mMediaPlayer.setDisplay(支架)再次不出现重新连接新创建的视图之后。黑色的屏幕显示,而不是视频。

The important line is mMediaPlayer.setDisplay(holder) because it ties the current view/display to the media player. The view (the "holder") is destroyed when you leave the activity. After returning to the activity and recreating the view, executing mMediaPlayer.setDisplay(holder) again doesn't appear to re-attach the newly created view. A black screen is shown instead of the video.

有没有人有一种变通方法或解决方案针对此问题。我想AP preciate任何帮助或建议。

Does anyone have a workaround or solution for this issue. I would appreciate any help or advice.

推荐答案

很多谷歌搜索周围,头刮了的状态图 的MediaPlayer的,我终于成功地避免这种所谓的黑屏。我已经张贴在OP的注释部分,这个问题似乎与最新的Andr​​oid版本(大于4.x的),所以我被选择加入到对姜饼设备类似的行为也得到解决。

After lot of googling around and head-scratching over state diagram of MediaPlayer, I finally managed to avoid this so called black screen. I've already posted in the OP's comments section that this problem seems to be resolved with latest Android versions (greater than 4.x)and so I was opting to have similar behavior on Gingerbread devices as well.

不用说, SurfaceHolder 回调方法起到的生命周期非常关键的作用有界的MediaPlayer - SurfaceView 的实施。而那些非常回调方法来得心应手我要走出这种局面。

Needless to say, SurfaceHolder callback methods play a very crucial role in the lifecycle of a bounded MediaPlayer-SurfaceView implementation. And those very callback methods came handy to me for getting out of this situation.

第一:

的onCreate(): - 基本初始化的东西..

inside onCreate(): - Basic initialization stuff..

    mVideoSurface = (SurfaceView) findViewById(R.id.videoSurface);
    videoHolder = mVideoSurface.getHolder();
    videoHolder.addCallback(this);

    // For Android 2.2

    videoHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    // Media Player and Controller

    player = new MediaPlayer();
    controller = new VideoControllerView(this);

随后, SurfaceView回调:

不要忘记设置显示的MediaPlayer 恕我直言, surfaceCreated ()是做的最好的地方。

don't forget to set the Display to your MediaPlayer and IMHO, surfaceCreated() is the best place to do that.

@Override
 public void surfaceCreated(SurfaceHolder holder) {
    player.setDisplay(holder);
    try {
    player.prepareAsync();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
}

这是最重要的事情。当用户通过pressing Home键或通过与期望任何结果打开一个不同的活动离开当前的活动,我们的 SurfaceView 将会被被摧毁。我想实现是从它被播放时的背景下得到了切换的位置继续进行中的视频播放。 因此,为了做到这一点,

And here's the most important thing. When user leaves the current activity by either pressing Home button or by opening a different activity with expecting any results, our SurfaceView is going to to be get destroyed. What I wanted to achieve was to resume the playback of ongoing video from the position it was playing when the context got switched. So, in order to do that,

将当前播放位置的变量,使我们可以在以后用它为征求我们播放到特定位置。 还有一。释放该死的的MediaPlayer 实例。我试图避免释放的MediaPlayer 实例,它的再创造,但我一直没有一遍又一遍。 So..point进行。

Save the current playback position in a variable so that we can use it later on to seek our playback to that particular position. And one more. Release the damn MediaPlayer instance. I tried to avoid releasing the MediaPlayer instance and it's re-creation but I keep failing over and over. So..point made.

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (player != null) {
        mCurrentVideoPosition = player.getCurrentPosition();
        player.release();
        player = null;
    }

}

现在,在prepared() 初始化任何的MediaController 在这里,如果你有兴趣。检查视频已经播放等寻求视频到该位置。

Now, onPrepared() Initialize any MediaController here if you're interested. Check if the video was already playing and so seek the video to that position.

     @Override
 public void onPrepared(MediaPlayer mp) {
    controller.setMediaPlayer(this);
    controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
    player.start();

    if (mCurrentVideoPosition != 0) {
        player.seekTo(mCurrentVideoPosition);
        player.start();
    }

}

最后,播放视频:

Finally, to play the video:

 void playVideo(){  

   try {
        if (player == null) 
            player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource("SOME_VIDEO_FILE.3gp");
        player.setOnPreparedListener(this);
        player.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.reset();
                return false;
            }
        });

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
     }

而这所有的一切。我知道这个问题是不存在的较新版本和所有伟大的,但...很多GB的是仍然在那里。

And that's all of it. I know the issue is not there with newer versions and all great but... lots of GBs are still out there.

这篇关于当返回到视频播放活动的Andr​​oid黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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