Skip to content Skip to sidebar Skip to footer

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 ar

Solution 1:

As mentioned previouslyyou 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

<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.youtubetester"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15" /><!--INTERNET and  ACCESS_WIFI_STATE permissions are required. --><uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".YouTubeTest"android:label="@string/title_activity_you_tube_test" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- You should include following part orientation is your choice--><activityandroid: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;

publicclassYouTubeTestextendsActivity {

    private Button button;
    @OverridepublicvoidonCreate(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(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                IntentlVideoIntent=newIntent(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

<RelativeLayoutxmlns: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" ><Buttonandroid: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.

Post a Comment for "Play Youtube Video In Videoview (within Application)"