integrate youtube channel in android - android

I want to integrate youtube channel in android such that when user open the channnel it displays all the videos of that channel..Below is the working code of integrating youtube video to android.Here I am providing video id inside loadVideo() method.But i don't know how to integrate channel..I want to know how to proceed in that ?
public class YoutubeActivity2 extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "";
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_api);
final ActionBar ab = getActionBar();
ab.hide();
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST)
{
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

You need to do API call to get all the videos from the channel.
Here's an example: https://github.com/youtube/yt-direct-lite-android/blob/master/app/src/main/java/com/google/ytdl/MainActivity.java#L391

Related

How to play next youtube video after ending 1 video in android

I am trying to play multiple videos after integrating YouTube player in android. I need to play another video after ending 1 one video in YouTube player I searched a lot but could not get any proper result.I don't know how I do this please help me.
public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "AIzaSyB2nIJ2lGZaCcvAq7a2ZY6Ny4lzjUhQld4";
private static final int RECOVERY_DIALOG_REQUEST = 1;
int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
YPlayer = youTubePlayer;
/*
* Now that this variable YPlayer is global you can access it
* throughout the activity, and perform all the player actions like
* play, pause and seeking to a position by code.
*/
if (!b) {
YPlayer.cueVideo("wPxqcq6Byq0");
YPlayer.cueVideo("wPxqcq6Byq0");
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
}
else { String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
}
#Override
public void onLoaded(String s) {
}
#Override
public void onAdStarted() {
}
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
}
Execute NextAsyncTask from onVideoEnded Override method.
This callback method call when current video is finished.
#Override
public void onVideoEnded() {
// do something here
// play next video here
}

play and pause ,video time indicator not displaying my YouTube API in android

I am using the YouTube API. It's working fine but play and pause and the video time indicator are not displaying.
Below is my code, can any one please help me?
public class PlayYouTube1st extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
String DEVELOPER_KEY = "AIzaSyjksmplqrQPzIDpWjdPeurjklmcJpbg";
// YouTube player view
private YouTubePlayerView youTubeView;
String youLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.youtube_main);
Intent intent =getIntent();
youLink=intent.getStringExtra("youtubeLink");
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Toast.makeText(PlayYouTube1st.this,
youLink, Toast.LENGTH_LONG).show();
// Initializing video player with developer key
youTubeView.initialize(DEVELOPER_KEY, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.loadVideo(youLink);
// Hiding player controls
player.setPlayerStyle(PlayerStyle.CHROMELESS);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
}
change YouTubePlayer style to :
player.setPlayerStyle(PlayerStyle.DEFAULT);
or
player.setPlayerStyle(PlayerStyle.MINIMAL);

onInitializationSuccess never called

My app plays youtube video without problem in Samsung S3 (4.3) but when I open it in HUAWEI ASCEND P8 (5.0), Nexus 5 (6.0) and BQ Aquaris M5 (5.0.2) the method onInitializationSuccess is never called
This is my code:
public class YoutubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
private String videoUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_player);
videoUrl = getIntent().getExtras().getString("url");
youTubeView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubeView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(this.videoUrl.split("=")[1]);
}
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.error), errorReason.toString());
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(API_KEY, this);
}
}
protected Provider getYouTubePlayerProvider() {
return youTubeView;
}
}
Could anybody hel me?
Thanks a lot

Integrating youtube playlists in android

I want to integrate youtube playlist in android such that when user open the playlist it displays all the videos of that playlist..Below is the working code of integrating youtube video to android.Here I am providing video id inside loadVideo() method.But i don't know how to integrate playlists..I want to know how to proceed in that ?
public class YoutubeActivity2 extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "";
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_api);
final ActionBar ab = getActionBar();
ab.hide();
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST)
{
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You should do an Data API call to get the videos of the channel then may create players with those video ids.
Here's an example for that call: https://github.com/youtube/yt-direct-lite-android/blob/master/app/src/main/java/com/google/ytdl/MainActivity.java#L391

java.lang.NoClassDefFoundError: while using YouTube API

11-26 15:50:58.159: E/AndroidRuntime(3199): java.lang.NoClassDefFoundError: com.android.PlayMedia
11-26 15:50:58.159: E/AndroidRuntime(3199): at com.android.Description.onClick(Description.java:63)
LIne Number 63
startActivity(new Intent(Description.this,PlayMedia.class));
PlayMedia.java
public class PlayMedia extends YouTubeFailureRecoveryActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play_media);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("wKJ9KzGQq0w");
}
}
#Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
}
YouTubeFailureRecoveryActivity.java(Provided by Youtube itself)
public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
}
}
protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();
}
I have registered the activity PlayMedia in my Manifest file.
Try this:
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.Mark all checkboxes.Click on Apply and clean the project.
This worked for me.Hope this helps.

Categories

Resources