Complete Tutorial how to play Youtube videos in Android Studio 1.5 with VideoView.

Create New Project with Application name: Play Youtube; Minimum SDK: API9 – Android 2.3 (Gingerbread).

In app->res->layout->activity_main.xml add the VideoView with code:

<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

In app->java->MainActivity complete the code (in onCreate):

package com.app.toyo.playyoutube;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView videoView =(VideoView)findViewById(R.id.videoView);
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri uri=Uri.parse("rtsp://r7---sn-4g57kue6.googlevideo.com/Ck0LENy73wIaRAmk3cJBg-iaXhMYDSANFC3u0pRWMOCoAUIJbXYtZ29vZ2xlSARSBXdhdGNoYIaluaTkzciOVooBCzVxRjNraG5XcXdnDA==/D693A8E7577C3A29E60C292B42C9C87D7C25A565.762A63DC4CA0A028DA83256C6A79E5F160CBEDA3/yt6/1/video.3gp");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }
}

In manifest->AndroidManifest.xml add the permissions:

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

Run your app in Emulator or better in Mobile device and see the Youtube video.

Tutorial how to play Youtube videos in Android with VideoView

How to get 3gp link of Youtube video:
For example we have the link www.youtube.com/watch?v=Xprog0HC3aQ
1) open new tab in your browser and write:  m.youtube.com/watch?v=VIDEO_ID&app=m
2) now change VIDEO_ID from above with your video id code (Xprog0HC3aQ) and the result should be: m.youtube.com/watch?v=Xprog0HC3aQ&app=m and press enter.
3) right click on your video and Copy Link Location.
4) the result: rtsp://r7—sn-4g57kue6.googlevideo.com/Ck0LENy73wIaRAmk3cJBg-iaXhMYDSANFC0k8JRWMOCoAUIJbXYtZ29vZ2xlSARSBXdhdGNoYKK205Ti2LaNVooBC2lZYlRtN201YTlRDA==/9EFB79E36D6A4191F3BF60D01A0AE429B5C9AA32.B774C6D2C0948C3A49A088F9F5CCCFBE34864B29/yt6/1/video.3gp
This is what you use on Uri.parse.
Watch the video tutorial how to play Youtube videos in Android with VideoView with Android Studio version 1.5:

Download free slideshow app from here and install it on your device to see an youtube video.

For further questions leave a message.

19 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Does not work in this method in Android Studio 1.4
Please tell me how it works in Android Studio 1.4?

    Tell me what error you have.

This method appears to work on a real device, but on an emulator (Android Studio 1.5.1) it gives a “Can’t play this video” error dialog. Would you happen to know why?

    Its may be due to the permissions that blocked by your anti virus

    me too, ???

On an emulator doesn’t work because of low computer resources.

the video is small n low quality how do i adjust that

how can i play a vimeo video using videoview since i have no idea of working with 3gp vimeo video.

is it legal??

Error:(13, 9) error: cannot find symbol class VideoView

Does not work
Eror
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
E/ViewRootImpl: sendUserActionEvent() mView == null

When we get 3gp link of Youtube video and play it on android phone it looks very dull. How can i make it clear. I am using 3gp link of video in android studio to play it on android phone.
Please help me.

Thanks for the detailed explanation, but how does it work for live videos ? I get an error on step3 of getting the 3gp link. Error : Live event playback is not supported on this client.

Congratulations for your tutorial. I used the tips that you passed and ran but the video put on YouTube when running int the application, it was not very good. It failed a few times. You could please help me how to solve this issue? Do I need to change something in the code so that the video has a better resolution? Grateful.

In my case youtube link is not convert into .3gp link please help
i am converting this link
“https://www.youtube.com/watch?v=Mr3FEVfjxxM”
to
“m.youtube.com/watch?v=Mr3FEVfjxxM
and convert this type link
“https://www.youtube.com/watch?v=Mr3FEVfjxxM&app=desktop”
i cannot copy link location and this link is not work in my case

Does it work as of now??

How to get Link adress programmatically if you have video id?

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*