Android:Fragment中的VideoView仅播放音频,不显示视频 [英] Android: VideoView within Fragment only playing audio, not displaying video

查看:93
本文介绍了Android:Fragment中的VideoView仅播放音频,不显示视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,允许用户从视频和图像内容的混合列表中进行选择.选择多媒体项目时,显示会根据媒体类型在ImageViewVideoView之间切换.问题是,当用户选择视频时,仅播放音频,而VideoView保持黑色.

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. The problem is, when the user selects a video, only the audio is played, and the VideoView remains black.

这是显示视频的代码:(底部的大多数代码都是反复试验,以查看是否缺少某些东西来解决此问题)

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是对活动的引用,该活动具有一些便捷功能,例如showLoader和诸如此类.)

(parent is a reference to the activity that has some convenience functions like showLoader and whatnot.)

这是我的布局:

<?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和整个Web上都进行了查找,但是在任何地方都找不到解决此问题的方法.感谢您的帮助.

I have looked all over StackOverflow and around the web for the last two days, but cannot find a solution to this problem anywhere. Any help is appreciated.

所有视频(和相关图像)都是远程的,它们不在Android设备上.

All of the videos (and images for that matter) are remote, they are not on the Android device.

推荐答案

尝试删除xml中定义的任何背景色.它可能会覆盖视频视图.

Try to remove any background color defined in the xml. It might cover the video view.

这篇关于Android:Fragment中的VideoView仅播放音频,不显示视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆