How to play video in VideoView?

和自甴很熟 提交于 2019-12-21 05:19:06

问题


I want to play video in VideoView. When I try to do that I got the following error:

Here is my source code:

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
         }  
    }
}

Here is my XML File:

<?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>

回答1:


Try to run app in Device , Video may not run in Emulator...




回答2:


see my answere here in the example code provided. If video is not playing try to re-encode it with a program like Handbrake and try again. Info on supported video formats: Android Supported Media Formats




回答3:


Try to add permission to connect to internet !

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



回答4:


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

Just call this function in your onCreate() method




回答5:


The answer to this problem may be in this post:

Returning false or not having an OnErrorListener at all will cause the OnCompletionListener to be called.

So return true instead of false from the function and no error will be shown, i.e.

video.setOnErrorListener(new OnErrorListener() {

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



回答6:


May be the url is not correct. Try to play that stream using vlc player. If it is played then we can look into other possible reason




回答7:


Ok first thing you wanna do is clean up your layout, this is your first problem:

unmesh it first and make the problem viewable:

You should have caught the error which is missing your closing cap after center", just add a ">" ... and that's it. Clean the project and you should see rtsp video. And your Internet Permission as previously stated.



来源:https://stackoverflow.com/questions/7710581/how-to-play-video-in-videoview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!