I keep getting following error- "There was a problem with network[400]".
Please refer the following code.
private void initializeYoutubeFragment() {
// in original code I am using my key here for sake of key safety I have replaced it
String apiKey = "MY_KEY";
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
YouTubePlayerSupportFragment youtubeFragment = YouTubePlayerSupportFragment.newInstance();
transaction.add(R.id.fl_video, youtubeFragment);
transaction.commitNow();
manager.executePendingTransactions();
youtubeFragment.initialize(apiKey, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cuePlaylist("5xVh-7ywKpE");
youTubePlayer.setPlayerStateChangeListener(callback);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
// no callback here
Log.e("TAG", "Fail");
}
});
}
See the attached screen shot
Update 1
I also added YouTubePlayer.PlayerStateChangeListener to youtubeplayer instance in onInitializationSuccess method. It calls onLoading method then goes on to call onError(YouTubePlayer.ErrorReason). Heer ErrorReason.INTERNAL_ERROR which have no explanation at all even in docs
callback code
private YouTubePlayer.PlayerStateChangeListener callback = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
Log.e("Tag", "loading");
}
#Override
public void onLoaded(String s) {
Log.e("Tag", "loaded");
}
#Override
public void onAdStarted() {
Log.e("Tag", "ad started");
}
#Override
public void onVideoStarted() {
Log.e("Tag", "Video Started");
}
#Override
public void onVideoEnded() {
Log.e("Tag", "video ended");
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
Log.e("Tag", "Error");
}
};
Related
I want to control play video by user not system , it plays automatically now. I try to use youTuBePlayer.pause(); but it's not working.
How do i let the video be loaded , it's status is on pause ?
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(getResources().getString(R.string.youTuBeApiKey), new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.loadVideo(commonEducationObject.getHD_YouTubeID());
//youTubePlayer.play();
youTubePlayer.pause();//it's not working
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
Have tried?
youTubePlayerFragment.initialize(Constants.API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
yPlayer = player;
yPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yPlayer.cueVideo(VIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
i found the solution !
youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
}
#Override
public void onLoaded(String s) {
youTubePlayer.pause();
}
#Override
public void onAdStarted() {
}
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
});
I have implemented a Youtube player using YouTubePlayerSupportFragment inside a ViewPager which I have did successfully. The problem which I am getting is YoutubePlayer getting paused in every second whenever I tried to play it. But if I play it in full screen then its playing perfectly but having issue playing inside View Pager. The code for the YouTubePlayerSupportFragment is:
private void initYoutubeFragment() {
final YouTubePlayerSupportFragment youTubePlayerFragment =
YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.fl_coach_profile_youtube, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(getString(R.string.youtube_player_api_key)
, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
mbIsVideoRestored = wasRestored;
if (!wasRestored) {
mYoutubePlayer = player;
mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
mYoutubePlayer.setPlaybackEventListener(mPlaybackEventListener);
mYoutubePlayer.setPlayerStateChangeListener(mPlayerStateChangeListener);
} else {
player.play();
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult error) {
}
});
}
Listeners for the player
// Youtube player event listener
private YouTubePlayer.PlaybackEventListener mPlaybackEventListener = 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() {
}
};
// Youtube player state change listener
private YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener = 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() {
}
};
Use release() method before moving to another Fragment:
youTubePlayer.release();
My unlisted (not private, just unlisted!) videos won't play. Setting the PlayerStateChangeListener revealed, that the videos won't play because of an INTERNAL_ERROR:
YouTubePlayerSupportFragment fragment = YouTubePlayerSupportFragment.newInstance();
getChildFragmentManager()
.beginTransaction()
.add(R.id.video, fragment)
.commit();
fragment.initialize(DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(videoIDForVideoMedia(MY_VIDEO_ID));
youTubePlayer.setPlayerStateChangeListener(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) {
Log.e(TAG, errorReason.toString()); // INTERNAL_ERROR right after the video loaded
}
});
// etc...
More about errors can be found here.
Any public video id I set for MY_VIDEO_ID plays without any problem so far. Has anyone played unlisted videos successfully before?
I have a YouTubeBaseActivity it works well, but when I click to go to another activity and then return to that activity, it does not auto play.
I would like it to resume and just start playing the lists again.
Here is the class below :
public class VideoPlayer extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,View.OnClickListener{
private YouTubePlayerView ytp_mainvideo;
private YouTubePlayer vyouTubePlayer;
private YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener;
boolean fullScreen =false;
TextView txtMainVideo;
List<String> videoList;
static String currentKey="";
Utilities utilities = new Utilities(this);
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save state
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if (vyouTubePlayer == null) {
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
}
}
#Override
protected void onStop() {
super.onStop();
if(vyouTubePlayer!=null)
vyouTubePlayer.cueVideos(videoList);
}
#Override
public 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.vidsplash);
currentKey = utilities.getLicenseKey();
// Initialize the Main Video Player.
ytp_mainvideo =(YouTubePlayerView)findViewById(R.id.youtubPlayerView);
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
txtMainVideo =(TextView)findViewById(R.id.text_mainvideo);
txtMainVideo.setOnClickListener(this);
Resources res = getResources();
String[] varray = res.getStringArray(R.array.videolist);
videoList = Arrays.asList(varray);
mPlayerStateChangeListener = 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() {
Log.d("ShowCase", "Video Ended");
if(vyouTubePlayer != null)
vyouTubePlayer.loadVideos(videoList);
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
};
if(currentKey.compareTo("")==0) {
Intent i = new Intent(getApplicationContext(), NewStartActivity.class);
startActivity(i);
}
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.text_mainvideo:
Intent intentsub=new Intent(getApplicationContext(),CateMainActivity.class);
startActivity(intentsub);
break;
}
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
vyouTubePlayer =youTubePlayer;
youTubePlayer.setPlayerStateChangeListener(mPlayerStateChangeListener);
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
youTubePlayer.loadVideos(videoList);
// Hiding player controls
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
#Override
public void onFullscreen(boolean _isFullScreen) {
fullScreen = _isFullScreen;
}
});
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Toast.makeText(appContext, "BAck", Toast.LENGTH_LONG).show();
AlertDialog.Builder alert = new AlertDialog.Builder(
VideoPlayer.this);
alert.setTitle(getString(R.string.app_name));
alert.setIcon(R.drawable.app_icon);
alert.setMessage("Are You Sure You Want To Quit?");
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//you may open Interstitial Ads here
finish();
}
});
alert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
First Declare YoutubePlayer & seekTime Variable
private YouTubePlayer youTubePlayer;
private int seekTime = 0;
Then in OnInitializationSuccess method set it to youtubePlayer
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
if (!wasRestored) {
youTubePlayer.loadVideo(videoId);
}
}
After that, You can store video current time in onPause and can resume the video in onResume method using the trick below
#Override
protected void onResume() {
super.onResume();
if (youTubePlayer == null) {
getYouTubePlayerProvider();
} else {
youTubePlayer.loadVideo(videoId, seekTime);
}
}
#Override
protected void onPause() {
super.onPause();
seekTime = youTubePlayer.getCurrentTimeMillis();
}
It worked fine for me, will work for you as well. Keep Coding :)
Try this
protected void onResume() {
super.onResume();
if (vyouTubePlayer == null) {
ytp_mainvideo.initialize(Config.DEVELOPER_KEY, this);
}
else {
vyouTubePlayer.Play();
}
}
I think it gets paused at OnPause, so you need to call Play()
The solution for me was to create a 2nd API key. This is very rare issue and I am thankful to Faceles for his responses which lead me to solve it. In this project I am using 2 Youtube Players and so when I moved to the other activity with the second player and returned to this first it never loaded.
I am using the YouTube API to play a video in my app.
I want to play the video right when the user enters the fragment so I used the command player.play(); in order to play automatically. However this command doesn't seem to work...
Here is my code:
public class YouTubeVideoFragment extends YouTubePlayerFragment{
YouTubePlayer player;
public YouTubeVideoFragment()
{
}
#Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
if(player!= null)
{
player.release();
}
}
#Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
if(player!= null)
{
player.release();
}
}
public static YouTubeVideoFragment newInstance(String url)
{
YouTubeVideoFragment f = new YouTubeVideoFragment();
Bundle b = new Bundle();
b.putString("url", url);
f.setArguments(b);
f.init();
return f;
}
private void init()
{
initialize("api_key", new OnInitializedListener() {
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1)
{
Log.e("YouTubeFragment", "Error");
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
if (!wasRestored)
{
player.cueVideo(getArguments().getString("url"));
player.play();//THIS DOESNT WORK!!!
}
}
});
}
}
If you need to load and play a video right after initialization of a fragment, you should simply use player.loadVideo(videoId) instead of player.cueVideo(videoId) and override onLoaded() hook.
Have a look at the docs:
loadVideo(String videoId)
Loads and plays the specified video.
cueVideo(String videoId)
Loads the specified video's thumbnail and prepares the player to play the video, but does not download any of the video stream until play() is called.
You have to call player.play() in onLoaded() of PlayerStateChangeListener().
So change onInitializationSuccess to this:
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
if (!wasRestored)
{
player.setPlayerStateChangeListener(new PlayerStateChangeListener() {
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onLoading() {
}
#Override
public void onLoaded(String videoId) {
player.play();
}
#Override
public void onError(ErrorReason reason) {
}
#Override
public void onAdStarted() {
}
});
}