user1551578
user1551578

Reputation: 73

Play Youtube video in VideoView (Within Application)

From past 2-3 weeks I am searching for a way to play youtube video in videoview and I have tried almost all the ways that are posted in stackoverflow. (Almost every... and there are lot I must tell). But none seems working for me.

I even tried android-youtube-player also but that doesn't works for me.

MY Requirement: Play Youtube video in VideoView because I want don't want to open Youtube app(A lot of reasons)

If some one is willing to share a working code than that would be of great help. I have tried almost everything and tired of CODING. Hope someone could help me out here.

Upvotes: 0

Views: 5973

Answers (1)

saji159
saji159

Reputation: 328

As mentioned previously you can play youtube videos with open youtube player.

I have attached the working sample here. (Drive is the best solution pops in to my mind and you can save the rar file by pressing ctrl+s). There you will find two projects.

  1. YouTubeTester - the sample app I have done
  2. OpenYouTubeActivity - the youtube player that downloaded form here

I have included the jar file of OpenYouTubeActivity project in to my project and if you prefer you can refer the OpenYouTubeActivity project as a library for your project (If you refer the project as a library make sure to remove the jar file.). The downloaded source of OpenYouTubeActivity has been updated as mention in issue list

VideoStream.java (Line: 30)
change: mUrl = lArgMap.get("url");
to:  mUrl = lArgMap.get("url") + "&signature=" + lArgMap.get("sig");

Now back to the sample project.

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.youtubetester"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <!--INTERNET and  ACCESS_WIFI_STATE permissions are required. -->
    <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:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".YouTubeTest"
            android:label="@string/title_activity_you_tube_test" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- You should include following part orientation is your choice-->
        <activity
            android:name="com.keyes.youtube.OpenYouTubePlayerActivity"
            android:screenOrientation="landscape" >
        </activity>
    </application>

</manifest>

YouTubeTest activity class

package com.youtubetester;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.keyes.youtube.OpenYouTubePlayerActivity;

public class YouTubeTest extends Activity {

    private Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_tube_test);
        button =(Button) findViewById(R.id.play);
        /*
         * The Youtube URL that we get is something like following.
         * http://www.youtube.com/watch?v=J467jzLlDcc
         * We need the last part of the URL or id of the video-J467jzLlDcc **/


        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent lVideoIntent = new Intent(null, Uri
                        .parse("ytv://"+"J467jzLlDcc"),
                        YouTubeTest.this,
                        OpenYouTubePlayerActivity.class);
                startActivity(lVideoIntent);
                /*
                 * Please note only the id has been passed and prefix is "ytv" NOT "ytpl"*/
            }
        });
    }


}

Layout - activity_you_tube_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".YouTubeTest" />

</RelativeLayout>

Hope this will help. Please ask if you have any problems. I don't have a deep understanding about the OpenYouTubeActivity project but I'll be able to help.

Upvotes: 9

Related Questions