Resume Android youtubePlayer in OnResume - android

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.

Related

youtube video stops playing after entering or exiting from picture in picture mode

I am trying to play youtube video, In Picture in Picture mode.
For this I am using youtube player api, when I press back button youtube player activity comes in picture in picture mode but video is stops playing, when i press play button then it again comes in full screen and start playing from start.
In full screen after rotating screen video is playing normally and start playing video where it before rotating.
I am not getting continues playing video in PIP.
Here is my code
public class YoutubePIPActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener
{
private static final String API_KEY = AppConstant.YUTUBE_PLAYER_API_KEY;
private static String mVIDEO_ID = "o7VVHhK9zf0";
private boolean isPIPModeEnabled = true;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_pip);
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(mVIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult)
{
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
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() {
}
};
#Override
public void onBackPressed()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
&& isPIPModeEnabled)
{
enterPIPMode();
}
else
{
super.onBackPressed();
}
}
private void enterPIPMode()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Rational rational = new Rational(250, 150);
PictureInPictureParams.Builder params = new PictureInPictureParams.Builder();
params.setAspectRatio(rational);
this.enterPictureInPictureMode(params.build());
} else
{
this.enterPictureInPictureMode();
}
new Handler().postDelayed(() -> checkPIPPermission(), 30);
}
}
#RequiresApi(Build.VERSION_CODES.N)
void checkPIPPermission()
{
isPIPModeEnabled = isInPictureInPictureMode();
if(!isInPictureInPictureMode())
{
onBackPressed();
}
}
#Override
protected void onUserLeaveHint()
{
super.onUserLeaveHint();
enterPIPMode();
}
#Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig)
{
if(newConfig !=null){
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
}
}
Here is activity defined in manifest
<activity android:name=".ui.activity.youtube.YoutubePIPActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:exported="false"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:theme="#style/Theme.AppCompat.FullScreen"
tools:targetApi="n"/>

Play video from youtube in picture-in-picture mode

I have an app which plays video from youtube and i have coded the link in my MainActivity class..
I have made the video played in my app as expected..
I have tried PictureInPictureParams.Builder but ran into multiple errors..
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
private MyPlayerStateChangeListener playerStateChangeListener;
private MyPlaybackEventListener playbackEventListener;
private YouTubePlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
playerStateChangeListener = new MyPlayerStateChangeListener();
playbackEventListener = new MyPlaybackEventListener();
final EditText seekToText = (EditText) findViewById(R.id.seek_to_text);
Button seekToButton = (Button) findViewById(R.id.seek_to_button);
seekToButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int skipToSecs = Integer.valueOf(seekToText.getText().toString());
player.seekToMillis(skipToSecs * 1000);
}
});
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
this.player = player;
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
if (!wasRestored) {
player.cueVideo("I0D-fkypQQw"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
}
}
#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.player_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(Config.YOUTUBE_API_KEY, this);
}
}
protected Provider getYouTubePlayerProvider() {
return youTubeView;
}
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {
#Override
public void onPlaying() {
// Called when playback starts, either due to user action or call to play().
showMessage("Playing");
}
#Override
public void onPaused() {
// Called when playback is paused, either due to user action or call to pause().
showMessage("Paused");
}
#Override
public void onStopped() {
// Called when playback stops for a reason other than being paused.
showMessage("Stopped");
}
#Override
public void onBuffering(boolean b) {
// Called when buffering starts or ends.
}
#Override
public void onSeekTo(int i) {
// Called when a jump in playback position occurs, either
// due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
}
}
private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
#Override
public void onLoading() {
// Called when the player is loading a video
// At this point, it's not ready to accept commands affecting playback such as play() or pause()
}
#Override
public void onLoaded(String s) {
// Called when a video is done loading.
// Playback methods such as play(), pause() or seekToMillis(int) may be called after this callback.
}
#Override
public void onAdStarted() {
// Called when playback of an advertisement starts.
}
#Override
public void onVideoStarted() {
// Called when playback of the video starts.
}
#Override
public void onVideoEnded() {
// Called when the video reaches its end.
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
// Called when an error occurs.
}
}
}
How do I implement picture in picture mode for the video where the video shrinks to the bottom right corner of the app..
Now i get is the youtue video played in the activity.. what i expect is to make the video play in picture-in-picture mode
I solved my problem using PictureInPictureParams as follows
First i created a Button (enter_pip) in my xml and in the OnClick i coded the following :
enter_pip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (android.os.Build.VERSION.SDK_INT >= 26) {
//Trigger PiP mode
try {
Rational rational = new Rational(youTubeView.getWidth(), youTubeView.getHeight());
PictureInPictureParams mParams =
new PictureInPictureParams.Builder()
.setAspectRatio(rational)
.build();
enterPictureInPictureMode(mParams);
} catch (IllegalStateException e) {
e.printStackTrace();
}
} else {
Toast.makeText(MainActivity.this, "API 26 needed to perform PiP", Toast.LENGTH_SHORT).show();
}
}
});
I also added the following attributes in my AndroidManifest to my PipActivity to support Picture in Picture in my Activity
<activity
android:name=".PipActivity"
android:launchMode="singleTask"
android:supportsPictureInPicture="true"
android:theme="#style/AppTheme.NoActionBar" />

Youtube player getting paused in every second which is inside View Pager

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();

Optimum way to implement youtube player into android application

I'm working on a android application and i get mp4 file urls from a json file. Now i want to play these files inside my app using a youtube player. After some research i managed to play my videos within my app. But when i go back from playing videos to other parts of my app,the app seems to be slowed down. I need to know if i'm doing this correctly.
This is my code used to play the video.
if (YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(activity).equals(
YouTubeInitializationResult.SUCCESS)
&& android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Intent intent = YouTubeStandalonePlayer
.createVideoIntent(activity, API_KEY,
video.getFile());
startActivity(intent);
}
from video.getFile(); i'm getting my video url.
i have used YouTubeAndroidPlayerApi.jar as the library.
1.Download YouTubePlyaer API https://developers.google.com/youtube/android/player/downloads/
Register your app on google developer console https://console.developers.google.com
Take an unique API Key and use that in your App.
use below code
public class AboutUs extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about_us);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(Constants.YOUTUBE_API_KEY, this);
initViews();
}
private void initViews() {
Button btnVisitMega = (Button) findViewById(R.id.btn_visit_megaforties);
Button btnVisitSecurity = (Button) findViewById(R.id.btn_visit_security_seals);
btnVisitMega.setOnClickListener(this);
btnVisitSecurity.setOnClickListener(this);
}
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(Constants.YOUTUBE_VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new 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 PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};

Play YouTubePlayer programmatically not working

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() {
}
});
}

Categories

Resources