如何在VideoView中播放视频?

问题描述 投票:4回答:7

我想在VideoView播放视频。当我尝试这样做时,我收到以下错误:

这是我的源代码:

package com.video.listitem;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class PlayVideo extends Activity {

     VideoView mVideoView;
     MediaController mc;
     String videourl;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.videoplay);

        try {
             mVideoView = (VideoView) findViewById(R.id.videoview);
             String videourl = "rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQl25yDUbxNXTRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
             mc = new MediaController(this);
             mVideoView.setMediaController(mc);
             mVideoView.requestFocus();
             mVideoView.setVideoURI(Uri.parse(videourl));
             mc.show();
             mVideoView.start();
         } catch (Exception e) {
         //TODO: handle exception
         }  
    }
}

这是我的XML文件:

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

    <VideoView
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:id="@+id/videoview" />

</LinearLayout>
android playback android-videoview
7个回答
3
投票

尝试在设备中运行应用程序,视频可能无法在模拟器中运行...


1
投票

在提供的示例代码中查看我的回答here。如果视频没有播放,请尝试使用Handbrake等程序对其进行重新编码,然后重试。有关支持的视频格式的信息:Android Supported Media Formats


1
投票

尝试添加连接到互联网的权限!

uses-permission android:name="android.permission.INTERNET"

0
投票
    public void playVideo() {
            videoView = findViewById(R.id.vid);
 videoView.setVideoPath("rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQl25yDUbxNXTRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp")
            videoView.start();
        }

只需在onCreate()方法中调用此函数即可


0
投票

这个问题的答案可能在this post

返回false或根本没有OnErrorListener将导致OnCompletionListener被召唤。

因此,从函数返回true而不是false,不会显示错误,即

video.setOnErrorListener(new OnErrorListener() {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Log.d("video", "setOnErrorListener ");
        return true;
    }
});

-3
投票

可能是网址不正确。尝试使用vlc播放器播放该流。如果它被播放,那么我们可以考虑其他可能的原因


-3
投票

好的,你要做的第一件事是清理你的布局,这是你的第一个问题:

首先解开它并使问题可见:

您应该已经捕获了错误,该错误在中心后缺少关闭上限“,只需添加”>“......就是这样。清理项目,您应该看到rtsp视频。以及您之前所述的Internet权限。

© www.soinside.com 2019 - 2024. All rights reserved.