[英]Android: VideoView within Fragment only playing audio, not displaying video
I have a fragment that allows users to select from a mixed list of video and image content. 我有一个片段,允许用户从视频和图像内容的混合列表中进行选择。 When selecting a multimedia item, display is toggled between an ImageView
and VideoView
depending on the media type. 选择多媒体项目时,将根据媒体类型在ImageView
和VideoView
之间切换显示。 The problem is, when the user selects a video, only the audio is played, and the VideoView
remains black. 问题是,当用户选择视频时,仅播放音频,并且VideoView
保持黑色。
This is the code for displaying video: (Most of the code at the bottom is trial-and-error to see if something is missing to fix this issue) 这是显示视频的代码:(底部的大多数代码都是反复试验,以确定是否缺少某些内容来解决此问题)
mainVideo.setVisibility(View.VISIBLE);
mainImage.setVisibility(View.INVISIBLE);
getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
parent.showLoader(R.string.buffering);
mainVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
parent.hideLoader();
mainVideo.requestFocus();
mainVideo.start();
}
});
mainVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
showErrorMessage(R.string.multimedia_load_failed, R.string.please_try_again_later);
parent.hideLoader();
return true;
}
});
Uri video = Uri.parse(mm.url);
mainVideo.setMediaController(new MediaController(parent));
mainVideo.setKeepScreenOn(true);
mainVideo.bringToFront();
mainVideo.setDrawingCacheEnabled(true);
mainVideo.setActivated(true);
mainVideo.setEnabled(true);
mainVideo.setVideoURI(video);
Log.i(TAG, "Loading video: " + video.toString());
( parent
is a reference to the activity that has some convenience functions like showLoader
and whatnot.) ( parent
是对具有一些便利功能的活动的引用,如showLoader
和showLoader
。)
Here is my layout: 这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_gravity="center_horizontal"
android:padding="1dp"
android:background="@color/light_grey">
<ImageView
android:id="@+id/mm_main_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/mm_main_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</VideoView>
</RelativeLayout>
</FrameLayout>
<TextView
android:id="@+id/mm_index_count"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="@dimen/large_padding"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"/>
<HorizontalScrollView
android:id="@+id/mm_horizontal_scroll"
android:layout_height="110dp"
android:layout_width="wrap_content">
<LinearLayout
android:id="@+id/mm_media_list"
android:layout_height="match_parent"
android:layout_width="wrap_content">
<!-- Content filled dynamically -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
I have looked all over StackOverflow and around the web for the last two days, but cannot find a solution to this problem anywhere. 我在过去的两天里一直在看StackOverflow和网络,但无法在任何地方找到解决这个问题的方法。 Any help is appreciated. 任何帮助表示赞赏。
All of the videos (and images for that matter) are remote, they are not on the Android device. 所有视频(以及相关图像)都是远程的,它们不在Android设备上。
Try to remove any background color defined in the xml. 尝试删除xml中定义的任何背景颜色。 It might cover the video view. 它可能会涵盖视频视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.