How to play video which is at some URL location in

2019-07-05 04:48发布

问题:

I am trying to play youtube video within video view but getting error(can't play this video) all the time(on device as well as on emulator too). Please help me with this problem. Thanks in advance.

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;

public class AndroidVideoView extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    VideoView myVideoView = (VideoView) findViewById(R.id.videoview);

    String viewSource = "rtsp://v2.cache8.c.youtube.com/CjYLENy73wIaLQndUnGVjs340xMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJP8mrqSz625UAw=/0/0/0/video.3gp";
    myVideoView.setVideoURI(Uri.parse(viewSource));
    myVideoView.requestFocus();
    myVideoView.start();

}

}

回答1:

1) Videos will not buffer on emulator because it is virtual device. In-order stream video you should use real device

2) you must provide this permission because with out internet connection we will not able to connect with YouTube server

 <uses-permission android:name="android.permission.INTERNET" />

3)Please validate your RTSP link. Given below is my rtsp link for my YouTube video

"rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";

To generate rtsp link Use this

http://gdata.youtube.com/feeds/mobile/videos/"+Video Id+"

In my case Video Id is sMM0R19IisI

My Code

public class MainActivity extends Activity {

String SrcPath = "rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";//"rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
    myVideoView.setVideoURI(Uri.parse(SrcPath));
    myVideoView.setMediaController(new MediaController(this));
    myVideoView.requestFocus();
    myVideoView.start();
   }
}

LAYOUT 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">

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

</LinearLayout>