I managed to get my video to play with VideoView but after I rotate my device video stops and starts playing from the beginning. I placed my VideoView inside fragment.
I tried this inside my fragment but without success:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onPause() {
super.onPause();
if(video.isPlaying()) {
video.pause();
progress = video.getCurrentPosition();
}
}
#Override
public void onResume() {
super.onResume();
if(video.isPlaying() && progress!=0) {
video.seekTo(progress);
video.start();
}
}
How to keep video playing after screen rotates?
Related
I have been working on playing videos using VideoView in Android.
OnPause of fragment i am pausing the video and storing the seektime
#Override
public void onPause() {
super.onPause();
if (videoview != null && videoview.isPlaying()) {
videoview.pause();
seekTime = videoview.getCurrentPosition();
}
}
OnResume i am trying to resume the video using -
#Override
public void onResume() {
super.onResume();
videoview.seekTo(seekTime);
//vvOnboardingVideos.resume();// this is not working for now - need to investigate
videoview.start();
}
The video plays from the beginning.
Is there anything i am doing wrong. Please suggest.
Try to reverse order of operations :
#Override
public void onResume() {
super.onResume();
videoview.start();
videoview.seekTo(seekTime);
}
try this:
save the video position when onpause() called
// This gets called before onPause
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("video_position", stopPosition);
}
Then in your onCreate() load the stopPosition from the bundle
#Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("video_position");
}
}
Resume the video using:
#Override
public void onResume() {
super.onResume();
Log.d("Tag", "onResume");
videoView.seekTo(stopPosition);
videoView.start(); //Or use videoView.resume() if it doesn't work.
}
I am creating a game, I want to play a background music for one activity only(For main menu of game), my code shown below, The problem is that the music plays more than one time, I want to play the same music also when activity Resumes.
public class Menu extends Activity {
MediaPlayer mp
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
public void play(View ButtonClicked) {
mp.stop();
mp.release();
//mp = MediaPlayer.create(Menu.this, R.raw.l);
//mp.start();
goToActivity(Game.class);
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
//coins
coin.setText(data.getString("coin"));
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
//mps.release();
}
In your onResume don't initialise MediaPlayer again and again. It creates new instance every time when you come to onResume. So add a check in onResume like this :
#Override
protected void onResume() {
super.onResume();
if (mp==null)
mp=MediaPlayer.create(MainActivity.this,R.raw.adalante);
if (!mp.isPlaying())
mp.start();
}
and additionally add this for prevention to play when activity goes to onPause
#Override
protected void onPause() {
super.onPause();
mp.pause();
}
i'm playing audio with Videoview. If try to change orientation during the player in pause state, the player loses this state and continues to play.
I have try to stop it but not succeeded.
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("pos", mediaView.getCurrentPosition());
outState.putBoolean("playerState", mediaView.isPlaying());
}
and on restore
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean playerState = savedInstanceState.getBoolean("playerState");
if(playerState) {
mediaView.start();
} else {
this.posOfMedia = mediaView.getCurrentPosition();
mediaView.pause();
}
}
has anybody an idea how can I keep pause state after rotation
I have a VideoView and it is pause when I start an Email intent. When the email intent is done, I want the videoView to continue playing, however, it restarts from the beginning.
#Override
public void onPause() {
Log.d(TAG, "onPause called");
super.onPause();
videoView.pause();
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.start();//resume() doesnt work
}
How can I get the videoView to resume from where it left off.
What about this:
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.seekTo(stopPosition);
videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}
// This gets called before onPause so pause video here.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("position", stopPosition);
}
Then in your onCreate() call the stopPosition from the bundle and set it globally
#Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("position");
}
Using seekTo gives a slight flickering efeect while resuming the video. Better approach would be using pause() and start() methods of MediaPlayer class instead of using methods from VideoView. The start() method of VideoView restarts the video from the beginning, but start() method of MediaPlayer resumes the video if the pause() method had been previously called.
Here is what official android doc says
public void start ()
Added in API level 1 Starts or resumes playback. If playback had
previously been paused, playback will continue from where it was
paused. If playback had been stopped, or never started before,
playback will start at the beginning.
You can get the MediaPlayer associated with a VideoView in OnPreparedListener of VideoView.
public class MainActivity extends Activity {
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
}
});
}
}
Then subsequent pause() and resume() methods can be called on MediaPlayer object itself.
//To pause the video
mMediaPlayer.pause();
//To resume the video
mMediaPlayer.start();
Very simple yet efficient solution. Hope it helps!
I am playing videos in my app, video should play portrait and landscape mode withought restarting actvity, please any can give examples are links.
using video view for playing video.
public class PlayVideoActivity extends Activity {
private VideoView video;
private ImageButton back;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
back = (ImageButton)findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
video=(VideoView)findViewById(R.id.videoView);
video.setDrawingCacheEnabled(true);
video.setDrawingCacheQuality(VideoView.DRAWING_CACHE_QUALITY_HIGH);
video.setVideoURI(Uri.parse("android.resource://"+ getPackageName() +"/" + R.raw.fillings_class_1));
video.requestFocus();
video.setMediaController(new MediaController(this));
video.start();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//restore the relevant information
}
}
If you really need to prevent the activity from being restarted on an orientation change, you need to set the configChanges attribute for the Activity in the manifest to include orientation.