3

我目前正在使用 VideoView 播放 rtsp 实时流媒体源。这工作正常。

VideoView 最初位于带有其他元素的片段内,处于“正常”状态,我正在尝试实现全屏切换按钮。

要进入全屏模式,我将 VideoView 从其父级(LinearLayout)中删除,然后将其添加到另一个 LinearLayout,使用 getActivity().addContentView() 添加到其他所有内容之上,代码如下:

    LayoutInflater lf = getActivity().getLayoutInflater();          
    vFullScreen = lf.inflate(R.layout.full_screen, myViewGroup, false);             

    LinearLayout fullscreenCont = (LinearLayout) vFullScreen.findViewById(R.id.fullscreen_container);

    ((ViewGroup) vsPlayer.getParent()).removeView(vsPlayer);

    fullscreenCont.addView(vsPlayer);

    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    getActivity().addContentView(vFullScreen, params);

问题是一旦从原始父视图中删除视频就会变黑。

我想要实现的是保留视频实例以避免再次重新连接/缓冲,但我不知道如何在父母切换期间保留视频播放,有什么想法吗?

编辑:

如果我暂停 videoView 然后恢复它,如下所示:

LayoutInflater lf = getActivity().getLayoutInflater();          
vFullScreen = lf.inflate(R.layout.full_screen, myViewGroup, false);             

LinearLayout fullscreenCont = (LinearLayout) vFullScreen.findViewById(R.id.fullscreen_container);

vsPlayer.getVideoView().suspend();
((ViewGroup) vsPlayer.getParent()).removeView(vsPlayer);

fullscreenCont.addView(vsPlayer);

LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getActivity().addContentView(vFullScreen, params);
vsPlayer.getVideoView().resume();

视频播放被中断(变黑几秒钟),但随后恢复,这好多了,但并不完美,因为恢复播放需要很长时间。

另一个不太好的部分是 VideoView 类的 suspend() 和 resume() 方法在 API 级别 8 及更高级别可用,我需要与 API 级别 7 兼容

4

2 回答 2

1

您的问题是您正在使用的 VideoView 变黑并在视图切换之间暂时徘徊吗?

如果是这样,我已经花了整整一周的时间试图找出导致该问题的原因。我无法找出实际原因,但我的解决方法确实阻止了 VideoView 通过屏幕更改持续存在:

public void hideVideoView(){        
    runOnUiThread(new Runnable() {
        public void run() {
            findViewById(R.id.yourVideoView).setVisibility(View.INVISIBLE);
        }
    });    
}

我基本上只是在视图切换时将视图设置为不可见,如果重新加载视图,我只需将其设置回 View.Visible

于 2012-06-04T15:27:24.623 回答
0

这可能不会:) 解决您的问题,但您可以在删除 VideoView 之前找到您所在的位置:

long progress = vsPlayer.getCurrentPosition(); 


LayoutInflater lf = getActivity().getLayoutInflater();          
vFullScreen = lf.inflate(R.layout.full_screen, myViewGroup, false);             

LinearLayout fullscreenCont = (LinearLayout)    vFullScreen.findViewById(R.id.fullscreen_container);

((ViewGroup) vsPlayer.getParent()).removeView(vsPlayer);

fullscreenCont.addView(vsPlayer);

LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getActivity().addContentView(vFullScreen, params);

vsPlayer.seekTo(progress);

前段时间我实际上遇到了同样的问题。我最终做的是将 VideoView 的 layoutparams 更改为 match_parent 而不是删除它。

mVideoView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
mVideoView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
于 2012-06-04T14:58:48.657 回答