Android:Fragment内的VideoView仅播放音频,不显示视频

2024-04-06

我有一个片段,允许用户从视频和图像内容的混合列表中进行选择。选择多媒体项目时,显示会在ImageView and 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是对具有一些便利功能的活动的引用,例如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 中定义的任何背景颜色。它可能会覆盖视频视图。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:Fragment内的VideoView仅播放音频,不显示视频 的相关文章

随机推荐