Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Youtube videos in Video View in android

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());
        }
like image 580
varghesekutty Avatar asked Jan 22 '14 09:01

varghesekutty


People also ask

Can ExoPlayer play YouTube video?

ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.

Why video is not playing in YouTube in Mobile?

Restart your device. Turn off your mobile data connection and then turn it on again. Clear the YouTube app's cache. Uninstall and reinstall the YouTube app.


1 Answers

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>
like image 151
Jun Avatar answered Sep 28 '22 02:09

Jun