我正面临着使用VideoView
闪烁的奇怪问题。当活动开始时,它会导致轻微的分数闪烁一秒钟。然后,视频开始。它在视频的顶部和底部显示2条黑线。请参阅下面的快照。
我在2台设备上测试了我的应用程序
1)三星n-8000(平板电脑)
2)联想a-800
video.xml
<?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="#ffffff"
android:gravity="center">
<VideoView
android:id="@+id/vvSplash"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#00ffffff">
</VideoView>
</LinearLayout>
活动代码:
private VideoView vd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video);
vd = (VideoView) findViewById(R.id.vvSplash);
playVideo();
}
private void playVideo() {
Uri uri = Uri.parse("android.resource://" + getPackageName() +"/"+ R.raw.intro);
vd.setVideoURI(uri);
vd.setMediaController(null);
vd.start();
}
任何帮助,将不胜感激。谢谢。
听起来很荒谬,但答案就在这里:
我重复解决方案,对那些发现的人表示赞赏:
<SurfaceView
android:layout_width="0px"
android:layout_height="0px"
android:visibility="gone" />
将表面视图添加到活动中的根视图,而不是直接父级是非常重要的。否则它将无法工作。
我也面临同样的问题,但通过将视频视图的布局参数从包装内容更改为匹配父级来解决它。您还需要从XML中删除视频视图的背景属性。我希望它对你有用
对于未来的人们:这个丑陋的小虫似乎已经在Marshmallow得到了解决。在我的工作中,我们正在使用Xamarin进行开发(因此,基本上,所有视图都以编程方式添加)。我们有一堆测试设备,我主要使用的是Marshmallow设备,因此在构建我正在处理的页面时,我从未注意到这种黑色闪烁。在最终使用Lollipop设备进行测试后,我注意到了这种闪烁。
不幸的是我没有解决方案。我们需要我们的应用程序尽可能跨平台,因此不鼓励使用布局xml / sadface。
如果按下后退按钮时你的问题是VideoView
闪烁,只需在调用super实现之前,在你的Activity的VideoView
方法中将INVISIBLE
可见性设置为GONE
或onBackPressed()
。
如果用户也可以通过“向上”按钮离开,拦截android.R.home
选项隐藏VideoView
。
这是Kotlin的一个例子:
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
android.R.id.home -> super.onOptionsItemSelected(item).also {
video_view.visibility = GONE // Hide videoView before leaving to avoid flickering
}
R.id.action_share -> consume { shareVideo() } // Your regular menu options
else -> super.onOptionsItemSelected(item)
}
override fun onBackPressed() {
video_view.visibility = GONE // Hide videoView before leaving to avoid flickering
super.onBackPressed()
}
只需从xml中删除以下行。
android:background="#00ffffff"
它会帮助你:)
对于任何仍然面临问题并且没有得到如何使用上述答案的人。它只是复制此代码并将其粘贴到您在主(第一)布局下使用videoview
的主活动布局中。
<SurfaceView
android:layout_width="0px"
android:layout_height="0px"
android:visibility="gone" />
在我的情况下
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Mainframlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/facebookbgcolor" >
<SurfaceView
android:layout_width="0px"
android:layout_height="0px"
android:visibility="gone" />
.....rest of the layout
改编自https://stackoverflow.com/a/27307286/38557的基于xml的解决方案,改为使用Java代码。在膨胀布局之前,将它放在Fragment的onCreateView()中:
// Add a SurfaceView to prevent flickering when the video is loaded later.
SurfaceView surfaceView = new SurfaceView(getActivity());
surfaceView.setVisibility(View.GONE);
container.addView(surfaceView, new ViewGroup.LayoutParams(0, 0));
它做同样的事情,并且可以在您不能或不想更改XML时使用。