简体   繁体   English

VideoView 只有音频,没有视频?

[英]VideoView audio only, no video?

I have a Video view in my activity used to display a video stored in my res.raw folder like this:我的活动中有一个视频视图,用于显示存储在 res.raw 文件夹中的视频,如下所示:

MediaController controller=new MediaController(this);
video.setMediaController(controller);
String filePath="android.resource://" + getPackageName() + "/" + R.raw.video3;
video.setVideoURI(Uri.parse(filePath));
video.requestFocus();
video.start();

The problem is that I can hear the audio only, but the video is not shown.问题是我只能听到音频,但没有显示视频。

What can be the reason for this?这可能是什么原因?

Edit: here's my layout:编辑:这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button 
    android:id="@+id/btnPlayAudio"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Play Audio"
        >
        </Button>
        <Button 
        android:id="@+id/btnPlayVideo"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Play Video"
        >
        </Button>

        <VideoView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/videoView"

        />
</LinearLayout>

OK I got it,好,我知道了,

the problem was that my VideoView had width and height set to wrap_content when I changed to fill_parent , the video appeared问题是当我更改为fill_parent时,我的 VideoView 的宽度和高度设置为wrap_content ,视频出现

thanks谢谢

Create a custom VideoPlayer by extending VideoView class and use it:通过扩展 VideoView 类创建自定义 VideoPlayer 并使用它:

public class VideoPlayer extends VideoView {

    public VideoPlayer(Context context) {
        super(context);
        init();
    }

    @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            TyrooLog.i(TAG, "onMeasure");
            int width = getDefaultSize(videoWidth, widthMeasureSpec);
            int height = getDefaultSize(videoHeight, heightMeasureSpec);
            if (videoWidth > 0 && videoHeight > 0) {
                if (videoWidth * height > width * videoHeight) {
                    TyrooLog.i(TAG, "video too tall, correcting");
                    height = width * videoHeight / videoWidth;
                } else if (videoWidth * height < width * videoHeight) {
                    TyrooLog.i(TAG, "video too wide, correcting");
                    width = height * videoWidth / videoHeight;
                } else {
                    TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
                }
            }
            TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
            setMeasuredDimension(width, height);
        }
    }
}

Your overcomplicating it :-)你把它复杂化了:-)

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

Linky: Play from Raw Resource Linky: 从原始资源播放

我的问题只发生在我使用模拟器时,当我在真实设备上尝试时它运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM