I am try to play video using youtube video API
When I put static ID means declare ID in file at that time code is working.
But when I try to fetch ID from getIntent() I also got ID but video not play.
I got There was problem with network.
Below is my code.
here below my code
private static final int RECOVERY_DIALOG_REQUEST = 1;
String DEVELOPER_KEY = "AryAGafadXF9ECWqjdPtsahshsrJHzg";
// YouTube player view
private YouTubePlayerView youTubeView;
String youtubeLink;
#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();
youtubeLink=intent.getStringExtra("youtubeLink");
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
// 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(youtubeLink);
// 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);
}
}
my error
not working I got 400 Network problem error
Main activity code
//youtube image display id and imageView
youtube_image=(ImageView)findViewById(R.id.revi_fulldes_movieyoutubeImg);
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
Its happens when you give youtube full url as parameter in loadVideo(videoID) method.
youtubeLink=intent.getStringExtra("youtubeLink");
//check is youtube videoID
//not give entire youtube url
Give ONLY the YouTube ID and not the entire YouTube URL (youtubeLink)to the loadVideo() method.
Related
Below is the code I have used inside the activity here I have embedded the youtube player!
In a nutshell, the videoId of the Live Stream is being passed to the youtube player from a youtube Data API call.
Whenever I try to play the live stream on the embedded layer, the video pauses after 2 seconds!
I created a demo app earlier to check the feasibility of embedding the YT Player. The video is streaming without any problems in that app! I have implemented the same code here but I am facing the above mentioned issue!
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_video);
youTubePlayerView = findViewById(R.id.youtubeView);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Bundle bundle = getIntent().getExtras();
String showVideo = bundle.getString(ConstantsYtPlayer.VIDEO_ID);
youTubePlayer.cueVideo(showVideo);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if(youTubeInitializationResult.isUserRecoverableError()){
youTubeInitializationResult.getErrorDialog(this, RECOVERY_REQUEST).show();
}else{
Toast.makeText(VideoActivity.this, "Error Initializing Youtube Player", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RECOVERY_REQUEST){
getYoutubePlayerProvider().initialize(API_KEY, this);
}
}
protected YouTubePlayer.Provider getYoutubePlayerProvider(){
return youTubePlayerView;}
}
Check your logcat. You cannot have views overlapping the YouTubePlayer.
I am getting this error while integrating YouTube player in my android app.
Error in on this line - youTubeView.initialize(Config.DEVELOPER_KEY, this);
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerView.initialize(java.lang.String, com.google.android.youtube.player.YouTubePlayer$OnInitializedListener)' on a null object reference
My Code:
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView youTubeView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_test);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
youTubeView = findViewById(R.id.youtube_view);
// Initializing video player with developer key
youTubeView.initialize(Config.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
youTubePlayer.loadVideo(Config.YOUTUBE_VIDEO_CODE);
// Hiding player controls
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
Toast.makeText(this, youTubeInitializationResult.toString(), 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(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
Get a API developer key from google api console and add the key in separate class as below example.
public class Config {
private Config() {}
public static final String DEVELOPER_KEY="AIzaSyAwhK2xxxxxxx-xxxxxxxxxxxE";
}
I am working in android application using android studio.
The you tube video show in video view but when start playing video it just play small part of video then stop work and I need to press play button to play smart part then stop work again and so on.
How can the video plat till end?
my code:
YouTubePlayerFragment myYoutubeFragment;
Button button;
YouTubePlayer.OnInitializedListener onInitializedListener;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_song);
myYoutubeFragment=(YouTubePlayerFragment)getFragmentManager().findFragmentById(R.id.youtubePlayerFragment);
myYoutubeFragment.initialize(API_KEY,this);
}
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason)
{
if(errorReason.isUserRecoverableError())
{
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
}
else
{
String errorMessage= String.format("Error in youtube",errorReason.toString());
Toast.makeText(this, errorMessage,Toast.LENGTH_LONG).show();
}
}
public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored)
{
//start buffering
if(!wasRestored)
{
player.cuePlaylist(PLAYLIST_ID);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == RECOVERY_DIALOG_REQUEST )
{
//retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(API_KEY,this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider()
{
return (YouTubePlayerView)findViewById(R.id.youtubePlayerFragment);
}
public void onResume()
{
super.onResume();
myYoutubeFragment.onResume();
}
#Override
public void onPause()
{
super.onPause();
myYoutubeFragment.onPause();
}
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
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