0

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.

user1551578
  • 73
  • 1
  • 2
  • 4

1 Answers1

9

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.

saji159
  • 328
  • 7
  • 14
  • are you sure to run above example in all the device ? or above Android Api 2.2 ? – Ashok Domadiya Mar 15 '13 at 11:26
  • :) Tested on 2.2 and above. – saji159 Mar 18 '13 at 06:48
  • However I believe you would need the youtube application to be installed on the device? – Akshat Agarwal Nov 15 '13 at 14:42
  • @AkshatAgarwal : I didn't test this on such a device. – saji159 Nov 18 '13 at 04:59
  • how can i find the id if the url of my video is like this : www.youtube.com/embed/PhNacT_Z34g ? – Libathos Nov 18 '13 at 15:22
  • @libathos: did you try to split the string and get the id. Check this post - http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – saji159 Nov 19 '13 at 09:42
  • @saji159: Hi, I know how to split strings I don't know which part of the string is the id. I've tried "PhNacT_Z34g" but it won't work – Libathos Nov 19 '13 at 10:00
  • @libathos: Your id is PhNacT_Z34g. I tested it using the sample application and it's working.code was as follows ("ytv://"+"PhNacT_Z34g") – saji159 Nov 19 '13 at 11:06
  • @saji159 It's working? I've just tried it and i says that it can't play the video. Could it be because of the Virtual Machine? – Libathos Nov 19 '13 at 13:35
  • @libathos: I tested it on my phone. – saji159 Nov 20 '13 at 04:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41519/discussion-between-saji159-and-libathos) – saji159 Nov 20 '13 at 10:05
  • I have tested your code in Genymotion Emulator It is working. But It is not working on actual device. My device has Android OS 6.0.1 and that emulator has 5.0.0. Can you give some tips or guidance what is the main problem?@saji159 – Maulik Dodia Nov 18 '16 at 10:12