Modify layout file, activity_main.xml, to add <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" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
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 com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailLoader.ErrorReason;
import com.google.android.youtube.player.YouTubeThumbnailView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, YouTubeThumbnailView.OnInitializedListener{
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
youTubeThumbnailView = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview);
youTubeThumbnailView.initialize(API_KEY, this);
youTubeThumbnailView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
Toast.makeText(getApplicationContext(),
"YouTubeThumbnailView.onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
Toast.makeText(getApplicationContext(),
"YouTubeThumbnailView.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader.setVideo(VIDEO_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
@Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
Toast.makeText(getApplicationContext(),
"ThumbnailLoadedListener.onThumbnailError()",
Toast.LENGTH_LONG).show();
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
Toast.makeText(getApplicationContext(),
"ThumbnailLoadedListener.onThumbnailLoaded()",
Toast.LENGTH_LONG).show();
}
}
}
Download the files.
Download APK to try on your device.
Next: Handle initialization error YouTube API Service
The Tutorial: YouTube Android Player API step-by-step
6 comments:
It is not working. it displayed "com.google.android.youtube.player.YouTubePlayerView" could not be instantiated.
You should have YoutubePlayerApi to run that
how load more thumbnails . i mean not just the one video's thumbnail , i need to show many videos thumbnail
hello everyone could you please help me I don't know how parse can get my videos from youtube and push them into android listview.
if i press thumbnail button the video is playing just 2 seconds then its stopping.
why??
great work thanks
Post a Comment