Play Youtube videos in Video View in android

22,591

Solution 1

Here is another working code

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>

Solution 2

You can't do that. Use YouTubeAndroidApi for playing YouTube videos.

Solution 3

There might be many reasons to not play the video,You can test it on other devices also set android:hardwareAccelerated="true" in manifest

try following listeners to get correct issue

playerview.setOnPreparedListener(preparelistener);
        playerview.setOnErrorListener(errorlistenr);
        playerview.setOnCompletionListener(completelistener); 
Share:
22,591
varghesekutty
Author by

varghesekutty

Updated on May 28, 2020

Comments

  • varghesekutty
    varghesekutty almost 4 years

    I am developing a youtube player in android. I am getting the rtsp video correct url. But still the video is not playing. Please help to me to find a solution.

    Thanks in advance

    Here is my code

    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());
            }