我有一个 fragment 允许用户从视频和图像内容的混合列表中进行选择。选择多媒体项目时,显示会根据媒体类型在 ImageView
和 VideoView
之间切换。问题是,当用户选择视频时,只播放音频,VideoView
保持黑色。
这是显示视频的代码:(底部的大部分代码都是反复试验,看看是否缺少某些东西来解决这个问题)
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
是对 Activity 的引用,该 Activity 具有一些方便的功能,如 showLoader
等等。)
这是我的布局:
<?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>
在过去的两天里,我查看了整个 StackOverflow 和整个网络,但找不到解决此问题的任何地方。感谢您的帮助。
所有视频(以及与此相关的图像)都是远程的,它们不在 Android 设备上。
最佳答案
尝试删除 xml 中定义的任何背景颜色。它可能会覆盖视频 View 。
关于Android:Fragment 中的 VideoView 只播放音频,不显示视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15481352/