Before create app using YouTube Android Player API, you have to "Download and import YouTube Android Player API" and "Register your app using YouTube Android Player API and obtain API Key".
Modify layout xml to add view of <com.google.android.youtube.player.YouTubePlayerView>.
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:autoLink="web" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Modify MainActivity.java, extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener.
package com.example.androidyoutubeapiplayer;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
}
Where API_KEY is the key obtained in the step of "Register your app using YouTube Android Player API and obtain API Key", VIDEO_ID is the ID of the video to play.
Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidyoutubeapiplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidyoutubeapiplayer.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Download the files.
Download APK to try on your device.
Next: YouTubeThumbnailView example of YouTube Android Player API
Related: YouTube Android Player API step-by-step
showing "error on initilisation of player"
ReplyDeleteit doesnt work, it says an error occured while initializing the player.
ReplyDeleteIt works .. thanks ...for error on initialisation of player ... u must have made smthn wrong with the jar file ... reread instructions and redo
ReplyDeleteTo run this app, Users need to run version 4.2.16 of the mobile YouTube app (or higher).Otherwise you will get "error on initilisation of player".
ReplyDeleteI am not sure where to place the xml with the id 'youtube_player'
ReplyDeleteDoes it replaced the generated activity_main.xml frame layout?
hello Anonymous,
ReplyDeleteyes.
Thanks for BLOG really gr8 blog.
ReplyDeleteI want to know if youtube app is not install then how can i play youtube videos.
Thanks
I want three you tube video or in one page..How to do it
ReplyDeleteCan we implement without extend this YoutubeBaseActivity ?
ReplyDeleteThanks
pls help me how to load multiple videos thumbnail and play eveyone
ReplyDeleteprivate YouTubePlayer.Provider getYouTubePlayerProvider() {
ReplyDelete// TODO Auto-generated method stub
return (YouTubePlayerView)findViewById(R.id."your_xml_id");
}
This worked for me.
will it work if I use eclipse and emulator, since I don't have any android phone. It's giving failure to initialize when I run in emulator, any specific emulator settings?
ReplyDeleteInteresting example!
ReplyDeleteAnyway, I couldn't play private video Id with this, is there any way that we can authenticate and play private videos using YoutubePlayerView?
Thanks in advanced.
I am problem when i run in hp. message error "java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.video_streaming/com.example.video_streaming.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.video_streaming.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.video_streaming-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.video_streaming-2, /vendor/lib, /system/lib]]"
ReplyDeletesteps I have done is in conformity with the tutorial. Please give a solution for those who have managed to do this tutorial
any body can explain me what wrong ? its not work !! this is my code
ReplyDelete"package evt.co.il.evtfamily.androidyoutubeapiplayer;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import evt.co.il.evtfamily.R;
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "AIzaSyAyfTfH50eIggZZCcwE85wgMq0dS4XKfl0";
public static final String VIDEO_ID = "o7VVHhK9zf0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
}
Great post. Very simple. Thanks!
ReplyDeletethanks it works
ReplyDeletetry it in your mobile it will work
Thanks a lot. This example is much cleaner than the one on the youtube developer site.
ReplyDeleteThanks,
Fabio
Hii Bro,
ReplyDeleteVery nice Artical. But i got struck here please have a look on that,
I have developed YouTube player when player running i want to close the player and move to last fragment on Back button pressed.
I am using this
public class YoutubeActivity : YouTubeBaseActivity, IYouTubePlayerOnInitializedListener
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.youtubeactivity_layout);
YouTubePlayerView youTubeView = FindViewById(Resource.Id.youtube_view);
youTubeView.Initialize("AIzaSyAQ9UQpJFUVHwk6ipyZ1Su0XvXzM7Oq-_4", this);
this._videoUrl = Intent.GetStringExtra("videoid");
var btnBack = FindViewById/Button/(Resource.Id.btnlogoBack);
btnBack.Click += (s, e) =>
{
Finish();
};
}
when i'm pressing back Activity not finishing properly it restart again.
And second thing is that from the same button i'm restarting another fragment or previous one Activity throwing null reference exception.
btnBack.Click += (s, e) =>
{
var contnt = new ContentFragment();
Android.Support.V4.App.FragmentTransaction fragmentTx= Activity.SupportFragmentManager.BeginTransaction();
fragmentTx.Replace(Resource.Id.crealtabcontent, contnt, "contentTag");
fragmentTx.Commit();
};
How should i solve that problem and to go back from here. :)
Tagged: youtube activity,back navigation