android – 如何正确关闭Landscape VideoView Activity?

前端之家收集整理的这篇文章主要介绍了android – 如何正确关闭Landscape VideoView Activity?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我有一个活动以横向模式播放http直播视频.

我的AndroidManifest.xml:

<activity android:name=".MediaPlayerActivity"
    android:label="@string/menu_player"
    android:launchMode="singleInstance"
    android:screenOrientation="landscape">
</activity>

我的活动布局:

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

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

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

更新原因
经过谷歌API的深入阅读后,我想我找到了造成这种恼人行为的原因,请查看here

Unless you specify otherwise,a configuration change (such as a change in screen orientation,language,input devices,etc) will cause your current activity to be destroyed,going through the normal activity lifecycle process of onPause(),onStop(),and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user,once onDestroy() is called in that instance then a new instance of the activity will be created,with whatever savedInstanceState the prevIoUs instance had generated from onSaveInstanceState(Bundle).

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

我仍在试图弄清楚如何因配置更改而绕过此活动娱乐.正如API中所述,重写
然而,onConfigurationChanged(Configuration),因为我在xml中明确定义了screenOrientation,所以没有调用onConfigurationChanged(),之前已经讨论了很多类似的SO,比如this one.

请提供正确方向的答案.

谢谢,
ÿ

解决方法

尝试在您的活动的onPause(),onResume()和onDestroy()方法添加对VideoView的suspend(),resume()和stopPlayback()的调用
@Override
protected void onResume() {
    mVideoView.resume();
    super.onResume();
}

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

@Override
protected void onDestroy() {
    mVideoView.stopPlayback();
    super.onDestroy();
}

VideoView类的实现因设备而异(并且类本身为very sparsely documented),但AOSP的Gallery3D代码确实调用MovieView activity lifecycle methods中的上述方法,所以希望大多数设备在这种情况下至少应该看起来很好.

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

原文链接:https://www.f2er.com/android/317048.html

猜你在找的Android相关文章