Youtube Player has been released when activity created on device rotate - android

YouTube player has been released when devices rotate and activity recreated. Then play video. Shows YouTube player has released. I don't know where it released. I have already set it to Null onDestroy Method.
`mPlayer.loadVideo(videoId);`
//pointing YouTube Player released

My solution is to save the current playing time by YouTubePlayer getCurrentTimeMillis() in onSaveInstanceState(Bundle state) when screen rotation gets triggered, and get the time back from onRestoreInstanceState(Bundle state).
Sample code would be:
static final String CURRENT_PLAY_TIME = "current_play_time";
int current_time;
#Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putInt(CURRENT_PLAY_TIME, youTubePlayer.getCurrentTimeMillis());
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
current_time = state.getInt(CURRENT_PLAY_TIME);
}
And after restoring the current playing time let YouTubePlayer load video with it.
youTubePlayer.loadVideo(VIDEO_ID, current_time);
Hope it helps!

A posible solution is make your app vertical or horizontal permanent
For doing this write within your manifest:
<android:screenOrientation="portrait"> //vertical
<android:screenOrientation="landscape"> //horizontal

I have found my Excelent solution..all going good with youtube player
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (mPlayer != null)
//Set YouTube Player here;
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mPlayer != null)
//Set YouTube Player here;
}
}

Related

How to save state(time when it was paused) of every video in exo player to resume it afterwards

I am not able to save the state of each video respectively. Currently, when I press back from a playing video, I save the last time of that video and when I click on any other video it resumes to the last saved position. But I want each position to be saved independently.
What I am doing:
#Override
public void onBackPressed() {
super.onBackPressed();
videoView.pause();
mPlayVideoWhenForegrounded = videoView.getPlayer().getPlayWhenReady();
// Store off the last position our player was in before we paused it.
mLastPosition = videoView.getPlayer().getCurrentPosition();
// Pause the player
videoView.getPlayer().setPlayWhenReady(false);
//Here I am saving the last position in the database
sqLiteDatabaseHandler.add(new Contact(mLastPosition,""));
}
#Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT > 23) {
videoView.resume();
}
//getting db values
results = (ArrayList) sqLiteDatabaseHandler.getAll();
if(results.size()>0){
for (int i = 0; i < results.size(); i++) {
//value from databse of mLastPosition saved in onBackPressed()
seekto=results.get(i).getmLastPosition();
}
//seekto returns int value as 5604 or 7400
if(seekto>1000){
videoView.getPlayer().seekTo(seekto);
}
}
}
What is the issue I am facing:
With the above logic, I am able to pause and resume the video from where left off. But it resumes each video from the mLastPosition saved of any video. I want to save each video position differently. Please give me a logic on how can I store each video updated mLastPosition to resume it from the same. Basically, I want each should resume from a different time where it was paused.
You need to store the current window, the current position, and possibly the state of playWhenReady each in a variable, so you can resume the player later. We can do something like the below:
Store the player position in onPause:
public void onPause() {
super.onPause();
player.setPlayWhenReady(false);
if (player != null) {
mLastPosition = player.getCurrentPosition();
…
}
}
Save the last playback position into the bundle:
public void onSaveInstanceState(Bundle currentState) {
super.onSaveInstanceState(currentState);
currentState.putLong(SELECTED_POSITION, mLastPosition);
currentState.putParcelable(KEY_TRACK_SELECTOR_PARAMETERS, trackSelectorParameters);
currentState.putInt(KEY_WINDOW, startWindow);
currentState.putLong(KEY_POSITION, startPosition);
currentState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
}
Initiate the player again in onResume and set seek to the last position of the player:
public void onResume() {
player.setPlayWhenReady(true);
super.onResume();
//initiateExoPlayerPlayer();
if(mLastPosition!=0 && player!=null){
player.seekTo(mLastPosition);
}
}
Have a look here.

Audio is playing twice in Android Media Player

In my application I'm having a method responsible for playing file placed under raw directory. But when Ever I call that function in my onResume() method, sound is played twice. Even I have googled and tried different solutions. even by checking mediaPlayer.isPlaying() and then stopping the MediaPlayer instance but still didn't get any help.
private void EnglishSound(){
if(mediaPlayer1!=null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
}
mediaPlayer1.reset();
mediaPlayer1.release();
}
mediaPlayer1 = MediaPlayer.create(this, R.raw.p012);
mediaPlayer1.start();
}
public void onResume() {
super.onResume();
EnglishSound();
}
and EnglishSound() is called no where else in the whole activity. Even have tried debugging but it never enters the if block containing isPlaying().
Try to release onPause()
public void onPause() {
super.onPause();
if(mediaPlayer1 != null)
mediaPlayer1.release();
}

Handle the seekTo function of media player in android

There is a video activity, I need to handle:
1) when the first time enter the activity, get the position from intent and play the video
2) keep the same position after rotate
Here is the code to handle first requirement
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_full_screen);
ButterKnife.bind(this);
if (getIntent() != null) {
video_url = getIntent().getStringExtra("video_url");
pos = (int)getIntent().getLongExtra("time", 0);
}
player.setOnPreparedListener(this);
player.setVideoURI(Uri.parse(video_url));
}
#Override
public void onPrepared(MediaPlayer mp) {
player.seekTo(pos);
player.start();
}
Here is the code to handle second requirement
#Override
protected void onSaveInstanceState(Bundle outState)
{
outState.putInt("time", (int)player.getCurrentPosition());
player.pause();
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
int last_pos = savedInstanceState.getInt("time");
player.seekTo(last_pos);
player.start();
super.onRestoreInstanceState(savedInstanceState);
}
The problem is the handling is conflict to each other.
If first time enter progess is 10s , when I play at e.g. 30s and rotate , it still go to 10s instead of 30s.
It is caused by the seekTo(pos) at the onPrepared function but I can not remove that as it handle the first requirement.
How to fix that? Thanks for helping.
Rotating the screen calls onDestroy(). Are you saving the time progress in onDestroy()? For more info on android life cycle see: https://androidcookbook.com/Recipe.seam?recipeId=2636

retain videoview in fragment when rotating

I have a activity who hold 2 fragments (one SlidingMenu and other a VideoPlayer with control and other views).
How Can I retain the video playing status when I'm rotating the device? the video is a HLS Stream, so, I don't need to start again the buffering when rotate.
I start playing with the savedInstanceState, but i can't get it work
If you are using the same resources on on different screen orientations, you can prevent fragment from recreating.
If you need to recreate fragment, you can store playback progress, and after recreation scroll video to stored position
#Override
protected void onPause() {
...
if(playbackWasStarted) {
video.pause();
videoProgress = video.getCurrentPosition();
}
...
}
#Override
protected void onResume() {
...
if(playbackWasStarted && videoProgress!=0) {
video.seekTo(videoProgress);
video.start();
}
...
}
EDIT Oh, I didn't noticed that this question is veeeery old...

Video don't play from same time when change screen orientation

I like this site very much because I can find many useful solutions help me very much especially I'm beginner in android programming and this is my first question so I hope find helping from this great community.
The Problem:
I play video in my app and make it playing in full screen in landscape when user orient screen so I want to make video restart playing from the same point by using seekTo() and getCurrentPosition() - but the problem that video play many seconds after the position I mean that video seek to same point in time but when start play it add many seconds.
My Code:
//initialise everything
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = (VideoView) findViewById(R.id.videoView1);
v.setVideoPath(dir + videoName + ".3gp");
v.setMediaController(new MediaController(this));
if (savedInstanceState == null){
v.start();
}
else {
playingTime = savedInstanceState.getInt("restartTime", 0);
v.seekTo(playingTime);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (file.exists()){
playingTime = v.getCurrentPosition();
v.stopPlayback();
outState.putInt("restartTime", playingTime);
}
}
I try many solution such as this
How to keep playing video while changing to landscape mode android
I'm using Galaxy Nexus to test my app .. I would be grateful for any hint.
Sorry for my bad English and thanks very much.
In your activity, keep everything normal. It boils down to saving the position when activity is recreated. Here is one approach:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialise everything
//finally
if(savedInstanceState!=null) {
int seekValue = savedInstanceState.getInt("where");
//now seekTo(seekValue)
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
int seekValue; //set this to the current position;
outState.putInt("where", seekValue);
super.onSaveInstanceState(outState);
}
}

Categories

Resources