Tarun Bisht
Tarun Bisht

Reputation: 131

Can't play YouTube video in my app

I want to play a YouTube video in my app. I'm confused now what to do. If you know any other method how to put YouTube video in apps then please tell.

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;
import android.widget.MediaController;

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://r2---sn-a5mekney.googlevideo.com/Cj0LENy73wIaNAlesqO_bJs9khMYDSANFC2Yi_JXMOCoAUIASARgnaGur5OBx_hXigELcTg4UmstWUFleWcM/1C1D9E3122CE052254D17B1F5AF58175E47748CF.26C6350D80F279F00F0B11480BA4C5D3E7F7F40A/yt6/1/video.3gp");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }

}

It shows the error cant play this video

manifest code is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alstom.motherstouch">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Upvotes: 1

Views: 184

Answers (1)

Mayur_Thakur
Mayur_Thakur

Reputation: 661

Try Android Youtube Player API

The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.

https://developers.google.com/youtube/android/player/

Upvotes: 1

Related Questions