Playing video from the pause state in VideoView - android

For custom videoview I had to override the MediaController (to change the design of the buttons and connect to the buttons pause / play a selector). The situation is this: put the video on pause and turning the application, and then again restored, the video starts playing again, and not from the point at which it was suspended, and therefore the question arose: how to make the video starts playing from the place , where it was stopped? What do I need to change in the VideoActivity?
public class VideoActivity extends Activity {
VideoView videoView;
VkMediaController mc;
private static final String CURRENT = "duration";
private static final String URL = "url";
private Uri mURI;
private int mCurrentPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_view_player);
videoView = (VideoView) findViewById(R.id.videoViewplayer);
mc = new VkMediaController(this);
mURI = getIntent().getData();
if (savedInstanceState != null) {
mURI = Uri.parse(savedInstanceState.getString(URL));
mCurrentPosition = savedInstanceState.getInt(CURRENT);
}
videoView.setMediaController(mc);
videoView.setVideoURI(mURI);
videoView.requestFocus();
}
#Override
protected void onStart() {
super.onStart();
videoView.start();
if (mCurrentPosition != -1) {
videoView.seekTo(mCurrentPosition);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(CURRENT, videoView.getCurrentPosition());
outState.putString(URL, mURI.toString());
super.onSaveInstanceState(outState);
}
}

I think the problem is that videoView.getCurrentPosition() will always return 0 when you call inside onSaveInstanceState(Bundle outState), cos at that time the video already reset, so call videoView.getCurrentPosition() when onPause(), this will return you the desired value.

I had the same problem. I thought I did everything right but it didnt work. Then I just changed the order so I first call VideoView.seekTo() and then VideoView.start() . That worked.
I also have VideoView.requestFocus() in there but I dont know if that matters.
videoPlayer.requestFocus();
videoPlayer.seekTo(position);
videoPlayer.start();
So if youre sure you position variable has the right value, this might be your answer.
You may not be able to seek when the Video is not loaded. Implement onPreparedListener like this:
public class VideoActivity extends Activity implements OnPreparedListener {
VideoView videoPlayer;
int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null){
position=savedInstanceState.getInt("pos")
}
// setting up the videoview
setContentView(R.layout.activity_video);
getIntent().getExtras().getString("url");
Uri videouri = Uri.parse(getIntent().getExtras().getString("url"));
videoPlayer = (VideoView) findViewById(R.id.videoView);
videoPlayer.setOnPreparedListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoURI(videouri);
}
/**
* Start the plaback when the video is loaded
*
* #param mp
* #see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
*/
public void onPrepared(MediaPlayer mp) {
//this is a TextView infront of the VideoView which tells the User the Video is loading- hide that
findViewById(R.id.tv_video_load).setVisibility(View.GONE);
videoPlayer.requestFocus();
videoPlayer.seekTo(position)
videoPlayer.start();
}
}

Portrait to LandScape LifeCycle Demo
VideoView videoView;
int lastPosition;
int Video_STATE_POSITION = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
lastPosition=savedInstanceState.getInt("STATE_POSITION");
videoView.seekTo(lastPosition);
videoView.start();
Log.e("Keshav", "MainActivity onCreate() is called savedInstanceState lastPosition->" + lastPosition);
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
outState.putString("message", "This is my message to be reloaded");
outState.putInt("STATE_POSITION", Video_STATE_POSITION);
super.onSaveInstanceState(outState);
}
#Override
protected void onPause() {
super.onPause();
Video_STATE_POSITION = videoView.getCurrentPosition();
Log.e("Keshav", "MainActivity onPause is called ->" +Video_STATE_POSITION);
}

Related

Android mediaplayer reinitializes on screen rotation

I am streaming/playing already downloaded video in the VideoView. When I rotate the screen all the objects are reinitialized so that the video is played from the beginning or if get current position and set the position on screen rotation the video is streamed again. As the video from back end is encrypted,it takes so much time to load it resulting in a very bad user experience.
The following is what I've os far.
public class MainActivity extends AppCompatActivity {
VideoView videoView;
Context context;
int stopPosition = 0;
private static MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
videoView = (VideoView) findViewById(R.id.videoView1);
if (savedInstanceState != null) {
stopPosition = savedInstanceState.getInt("position");
Log.d("", "savedInstanceState called" + stopPosition);
}
mediaController = new MediaController(context);
mediaController.setAnchorView(videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.videotest;
Uri uri = Uri.parse(path);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
#Override
public void onPause() {
Log.d("", "onPause called");
super.onPause();
videoView.pause();
}
#Override
protected void onResume() {
super.onResume();
Log.d("", "onResume called" + stopPosition);
videoView.seekTo(stopPosition);
videoView.resume();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
videoView.pause();
outState.putInt("position", 100);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
stopPosition = savedInstanceState.getInt("position");
}
}
When I used the following methods this problem is solved but some new one starts.
If I add android:configChanges="screenSize|orientation" to AndroidManifest.xml it works perfectly except onSaveInstanceState is not called. I've some works to do in it so I want it be called.
Another method I used is that using Mediaplayer and SurfaceView and it works too except the mediaplayer not functioned correctly in some cases(Video size changes in each videos)
Basically what I would like to have is that in above code onRestoreInstanceState should be called and video shouldn't reinitialized(or play without a delay) in screen rotation
Found a solution, if we add android:configChanges="screenSize|orientation" to manifest.xml then only onConfigurationChanged override is called, activity is not recreated. As I had one VideoView and a ListView in the screen which has parent LinearLayout, I changed orientation to horizontal from verical in onConfigurationChanged.
You have to dynamically set required views in Portrait or Landscape mode accordingly.

How to pause Android VideoView after 4 seconds of playing

I Want to pause my video play after 4 seconds of playing, then I want to pop up a message to the user. Can someone point me in the right direct how to pause the video after 4 seconds please. Thanks
V2 Implemented #gogothebee feedback {Now the video pause at position 0 (before it actually start).
public class PlayVideoActivity extends ActionBarActivity {
public static final String TAG = PlayVideoActivity.class.getSimpleName();
Bundle bunde;
Intent intent;
String content_actual_content;
VarController vc = new VarController(PlayVideoActivity.this);
public ImageLoader imageLoader;
private VideoView mVideoView;
private MediaController mMediaController;
ProgressBar pbplay1;
private boolean isActive;
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video); //playvideo (FullScreen)
handler = new Handler();
//Hide Action Bar
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();
//Set ProgressBar
pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);
//Get Link from ImageAdapter
intent = this.getIntent();
bunde = intent.getExtras();
content_actual_content = bunde.getString("content_actual_content");
Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();
//Initiate Player
mVideoView = (VideoView) findViewById(R.id.videoview);
mVideoView.setVideoURI(Uri.parse(content_actual_content));
mVideoView.setMediaController(new MediaController(PlayVideoActivity.this));
mVideoView.requestFocus();
mVideoView.setMediaController(mMediaController);
//new myAsync().execute();
}//--- end onCreate
#Override
protected void onPause() {
super.onPause();
isActive = false;
}
#Override
protected void onResume() {
super.onResume();
isActive = true;
mVideoView.requestFocus();
mVideoView.start();
scheduleVideoPause(4000);
}
private void scheduleVideoPause(int msec) {
handler.removeCallbacksAndMessages(null);
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!isActive) {
return;
}
mVideoView.pause();
Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
}
}, msec);
}
V1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video); //playvideo (FullScreen)
//Hide Action Bar
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();
//Set ProgressBar
pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);
//Get Link from ImageAdapter
intent = this.getIntent();
bunde = intent.getExtras();
content_actual_content = bunde.getString("content_actual_content");
Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();
//Initiate Player
mVideoView = (VideoView) findViewById(R.id.videoview);
mVideoView.setVideoURI(Uri.parse(content_actual_content));
mMediaController = new MediaController(PlayVideoActivity.this);
mVideoView.setMediaController(mMediaController);
mVideoView.requestFocus();
mVideoView.start();
//new myAsync().execute();
Log.i("**********************", "getCurrentPosition: " + mVideoView.getCurrentPosition());
if (mVideoView.getCurrentPosition() == 4000) {
mVideoView.pause();
Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
}
}//--- end onCreate
It seems that there's no way to do it easily. However, my solution is to spawn a background timer that runs every 100ms (adjustable) and check the current position. Once it is past the desired threshold (4000ms in this case), the video is stopped. I know it's not elegant and won't work exactly at 4000ms, but if anyone has a better idea, please share it. When adjusting the pooling interval in case you want to get as close as possible to 4000ms, don't go too low for performance reasons.
This solution can also be refactored as it's quite messy, but I wanted to show the concept in as little code as possible.
private Timer timer;
private boolean isActive;
private VideoView mVideoView;
private final static int POOLING_INTERVAL_MS = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initiate Player
mVideoView = (VideoView) findViewById(R.id.videoview);
mVideoView.setVideoURI(Uri.parse("Your-Video-URI"));
mVideoView.setMediaController(new MediaController(this));
}
#Override
protected void onPause() {
super.onPause();
isActive = false;
cancelProgressPooling();
}
#Override
protected void onResume() {
super.onResume();
isActive = true;
mVideoView.requestFocus();
mVideoView.start();
initVideoProgressPooling(4000);
}
private void initVideoProgressPooling(final int stopAtMsec) {
cancelProgressPooling();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
mVideoView.post(new Runnable() {
#Override
public void run() {
if (!isActive) {
cancelProgressPooling();
return;
}
if(mVideoView.getCurrentPosition() >= stopAtMsec) {
mVideoView.pause();
cancelProgressPooling();
Toast.makeText(MainActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
}
}
});
}
}, 0, POOLING_INTERVAL_MS);
}
private void cancelProgressPooling() {
if(timer != null) {
timer.cancel();
}
timer = null;
}

How to keep video inside fragment playing after rotation?

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?

landscape mode video activity is restarting

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.

Videoview Restarts after pressing home/power button on phone

Below are my codes. My videoview restarts everytime you press on the home/power button. Are there any way i can make it continue where my video has been playing before pressing the home/power button.
Edited : I have put in the codes mentioned by Bharat. But it seems that I'me getting error vd not resolved.
public class Video1 extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videomain);
//AdView adView = (AdView)this.findViewById(R.id.adView);
//adView.loadAd(new AdRequest());
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.opening);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
vd.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
Intent intent=new Intent(Video1.this,GalleryVideo.class);
startActivity(intent);
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onPause() {
vd.pause(); /**ERROR - vd cannot be resolved**/
super.onPause();
}
#Override
protected void onResume() {
vd.resume(); /**ERROR - vd cannot be resolved**/
super.onResume();
}
}
Try this in your class
#Override
protected void onPause() {
vd.pause();
super.onPause();
}
#Override
protected void onResume() {
vd.resume();
super.onResume();
}
when your activity will go on pause it will pause your video view. when your activity will resume it will resume it in on resume.

Categories

Resources