如何正确关闭Landscape VideoView Activity?

yor*_*rkw 9 android android-videoview

在我的应用程序中,我有一个活动以横向模式播放http直播视频.

我的AndroidManifest.xml:

<activity android:name=".MediaPlayerActivity"
    android:label="@string/menu_player"
    android:launchMode="singleInstance"
    android:screenOrientation="landscape">
</activity>
Run Code Online (Sandbox Code Playgroud)

我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:background="@color/black">

  <VideoView android:id="@+id/myVideoView"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:layout_gravity="center_horizontal" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

问题是我每次关闭此活动(通过单击后退按钮),它总是旋转到纵向模式(时间很快,但实际上可以在返回到上一个活动之前看到对真实设备的影响),然后关闭.我该如何解决这个恼人的问题?

更多信息更新
此烦人的行为只发生如果上一个活动是纵向模式,如果前一个活动是横向,它就没事了.对我而言,当使用不同的screenOrientation设置淡入/淡出活动时,它看起来与Android框架有关.

原因更新
经过Google API的深入阅读后,我想我找到了造成这种烦人行为的原因,请点击此处:

除非您另行指定,否则配置更改(例如屏幕方向,语言,输入设备等的更改)将导致您的当前活动被破坏,通过onPause(),onStop()的正常活动生命周期过程,以及onDestroy()视情况而定.如果活动位于前台或对用户可见,则在该实例中调用onDestroy()后,将创建活动的新实例,以及上一个实例从onSaveInstanceState(Bundle)生成的任何savedInstanceState.

那么当单击后退按钮时幕后发生的事情:currnet VideoView Activity(landscape)被破坏,由于screenOrientation配置已经更改而创建了一个新的VideoView Activity(肖像),并且会立即去除(你可以看到屏幕上的效果) ,显示堆栈中的最后一个活动.这也解释了为什么如果最后一个活动具有相同的screenOrientation设置,这种恼人的行为就会消失.

我仍然试图弄清楚如何因配置更改而绕过此活动娱乐.正如API中所述,重写onConfigurationChanged(配置),但是,由于我在xml中明确定义了screenOrientation,因此未调用onConfigurationChanged(),之前已经讨论了很多类似的SO,就像这个一样.

请提供正确方向的答案.

谢谢,
Y

Joe*_*Joe 17

尝试添加到VideoView的电话suspend(),resume()stopPlayback()在您的活动的onPause(),onResume()onDestroy()方法:

@Override
protected void onResume() {
    mVideoView.resume();
    super.onResume();
}

@Override
protected void onPause() {
    mVideoView.suspend();
    super.onPause();
}

@Override
protected void onDestroy() {
    mVideoView.stopPlayback();
    super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

VideoView类的实现因设备而异(并且类本身的文档非常稀疏),但AOSP的Gallery3D代码确实在其MovieView活动生命周期方法中调用了上述方法,所以希望大多数设备至少应该让自己看起来很好在那种情况下.

如果它仍然看起来很糟糕,你可能想要onBackPressed()在你的活动中覆盖可能隐藏VideoView或一些类似的黑客来隐藏恼人的行为:)