片段内的 VideoView 导致黑屏闪烁 [英] VideoView inside fragment causes black screen flicking
问题描述
我们有一个在 Android API 4.0 上运行的 Android 应用程序.其中一项活动的布局将屏幕分为两部分.在左侧,我们有一个带有一些按钮的静态部分.在右侧,我们有一个 FrameLayout,它会根据按下的按钮切换到相应的片段.
We have an Android application running on Android API 4.0. One of the activities has a layout that divides the screen in 2 parts. On the left side we have a static part with some buttons. On the right side we have a FrameLayout that will switch to the corresponding fragment depending on the button that is pressed.
问题:右侧的片段之一包含 VideoView.当用户单击按钮以显示此片段时,会显示该片段,并且视频会立即开始播放:在渲染此片段时,整个屏幕会以黑色闪烁,这非常烦人.
Problem: One of the fragments on the right side contains a VideoView. When the user clicks on the button to show this fragment the fragment is shown and the video immediately starts playing however: upon rendering this fragment the complete screen flickers in black which is very annoying.
这是 VideoFragment 类的一些代码:
Here is some code of the VideoFragment class:
public class VideoFragment extends Fragment {
public VideoView videoView;
private ExternalVideo mVideo;
private boolean mVideoExists;
private String mDestination;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDestination = ApplicationParams.MEDIA_STORAGE_PATH + "videos/"
+ FilenameUtils.getBaseName(((DetailActivity)getActivity()).getProduct().getVideoUrl())
+ "."
+ FilenameUtils.getExtension(((DetailActivity) getActivity()).getProduct().getVideoUrl());
File file = new File(mDestination);
mVideoExists = file.exists() && file.isFile();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
if (mVideoExists) {
getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
view = inflater.inflate(R.layout.video, null);
mVideo = new ExternalVideo(mDestination);
videoView = (VideoView) view.findViewById(R.id.video_video);
MediaController mediaController = new MediaController(getActivity());
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath(mVideo.getFullPath());
videoView.requestFocus();
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
} else {
TextView textView = new TextView(getActivity());
textView.setText(getActivity().getString(R.string.videofragment_video_coming_soon));
textView.setPadding(50, 50, 50, 50);
view = textView;
}
return view;
}
}
这是视频片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView android:id="@+id/video_video"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent"/>
</RelativeLayout>
有谁知道在渲染包含 VideoView 的片段时可能导致此闪烁问题的原因是什么?一个解决方案将不胜感激!
Does anyone have an idea what could be causing this flickering issue upon rendering the fragment that contains the VideoView? A solution would be appreciated!
如果我点击屏幕上 VideoView 所在的片段,一开始就会闪烁.当我导航到另一个片段并返回到包含 VideoView 的片段时,闪烁消失了.
If I click on the fragment where the VideoView is on the screen flickers in the beginning. When I then navigate to anoter fragment and go back to the one containing the VideoView the flicker is gone.
推荐答案
我能够通过在片段的父 Activity 的布局中添加 0px x 0px SurfaceView 来解决此问题:
I was able to fix this by adding a 0px by 0px SurfaceView in the layout of the parent activity of the fragment:
<SurfaceView
android:layout_width="0px"
android:layout_height="0px" />
这篇关于片段内的 VideoView 导致黑屏闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!