我们有一个在Android API 4.0上运行的Android应用程序。其中一项 Activity 的布局将屏幕分为两部分。在左侧,我们有一个带有一些按钮的静态零件。在右侧,我们有一个FrameLayout,它将根据所按下的按钮切换到相应的片段。
问题:
右侧的片段之一包含VideoView。当用户单击按钮以显示该片段时,将显示该片段,但视频立即开始播放:渲染该片段后,整个屏幕会以黑色闪烁,这非常令人讨厌。
这是VideoFragment类的一些代码:
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的片段时会导致此闪烁问题的原因?一个解决方案将不胜感激!
编辑1:如果我单击屏幕上VideoView所在的片段,开始时会闪烁。然后,当我导航到注释片段并返回到包含VideoView的片段时,闪烁消失了。
最佳答案
我可以通过在片段的父 Activity 的布局中添加一个0px x 0px SurfaceView来解决此问题:
<SurfaceView
android:layout_width="0px"
android:layout_height="0px" />
关于android - fragment 内的VideoView导致黑屏闪烁,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17717906/