在Android视频视图中播放Youtube视频

时间:2014-01-22 09:15:42

标签: android video youtube

我正在使用android开发一个youtube播放器。我正在获取rtsp视频正确的网址。但仍然没有播放视频。请帮助我找到解决方案。

提前致谢

这是我的代码

String youtubeURL="rtsp://v6.cache4.c.youtube.com/CigLENy73wIaHwmh5W2TKCuN2RMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp";
  @Override
protected void onCreate(Bundle savedInstanceState)
{
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_online_video_player);

       videoView = (VideoView) findViewById(R.id.video_View);

       progressDialog = ProgressDialog.show(OnlineVideoPlayer.this, "", "Buffering video...",true);
       progressDialog.setCancelable(false);


       PlayVideo();
}

private void PlayVideo()
{
       try {
           final VideoView videoView =(VideoView)findViewById(R.id.video_View);
     //1   //mediaController = new MediaController(Splashscreen.this);
     //2   //mediaController.setAnchorView(videoView);
           // Set video link (mp4 format )
           Uri video = Uri.parse(youtubeURL);
           //videoView.setMediaController(mediaController);
           videoView.setVideoURI(video);
           videoView.setOnPreparedListener(new OnPreparedListener() {
               public void onPrepared(MediaPlayer mp) {
                   progressDialog.dismiss();
                  videoView.start();
               }
           });

        }catch(Exception e){
             progressDialog.dismiss();
            System.out.println("Video Play Error :"+e.getMessage());
        }

3 个答案:

答案 0 :(得分:6)

这是另一个工作代码

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_view);

    VideoView videoView =(VideoView)findViewById(R.id.videoView);
    MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);
    Uri uri=Uri.parse("rtsp://r2---sn-a5m7zu76.c.youtube.com/Ck0LENy73wIaRAnTmlo5oUgpQhMYESARFEgGUg5yZWNvbW1lbmRhdGlvbnIhAWL2kyn64K6aQtkZVJdTxRoO88HsQjpE1a8d1GxQnGDmDA==/0/0/0/video.3gp");
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(uri);
    videoView.requestFocus();

    videoView.start();


}

activity_video_view.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/videoView"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/>
</LinearLayout>

答案 1 :(得分:3)

你做不到。使用YouTubeAndroidApi播放YouTube视频。

答案 2 :(得分:2)

可能有很多理由不播放视频,您可以在其他设备上进行测试    android:hardwareAccelerated =清单

中的“true”

尝试关注侦听器以获得正确的问题

playerview.setOnPreparedListener(preparelistener);
        playerview.setOnErrorListener(errorlistenr);
        playerview.setOnCompletionListener(completelistener);