VideoView片段内导致黑屏轻弹 [英] VideoView inside fragment causes black screen flicking
问题描述
我们必须运行在Android 4.0的API Android应用程序。活动之一具有一个布局,将屏幕分为两部分。在左侧,我们有一个静态部分用一些按钮。上的右侧,我们有一个的FrameLayout将切换到根据按钮的相应片段即pressed
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类的一些code:
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;
}
}
下面是视频片段的布局:
Here is the layout of the video fragment:
<?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片段这可能是造成这种闪烁问题?一个解决方案是AP preciated!
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!
EDIT1:如果我点击的片段,其中VideoView是在屏幕上闪烁在开始的。当我再浏览到anoter片段,并返回到含有闪烁消失的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.
推荐答案
我能够在片段的父活动的布局添加0px 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屋!