Videoview Restarts after pressing home/power button on phone - android

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.

Related

VideoView stopping on Dialog Open of anykind

I have a video view in an activity.
private void videoInit() {
videoView = (VideoView)findViewById(R.id.videoView);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
mp.setLooping(true);
}
});
videoView.setVideoPath("android.resource://com.aplos.ideliver2/raw/iphone_inner_animation");//src/main/res/raw
videoView.requestFocus();
videoView.start();
}
This is my function, and the dialog is opened with an intent to the other class.
This is executed for a scenario.
private void processError(Response mResponse) {
try {
AlertDialog.strMessage = mResponse.getResponseMessage();
AlertDialog.strTitle = "Alert";
AlertDialog.strButtonTitle = "OK";
startActivity(new Intent(context, AlertDialog.class));
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} catch (Exception e) {
H.Handle(e);
}
}
When the dialog is opened the video stops. I want to have the video running continuosly.
#Override
public void onBackPressed() {
super.onBackPressed();
videoView.stopPlayback();
}
#Override
protected void onPause() {
super.onPause();
videoView.stopPlayback();
}
#Override
protected void onResume() {
super.onResume();
videoView.start();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
videoView.start();
}
Remove these two
#Override
protected void onPause() {
super.onPause();
videoView.stopPlayback();
}
#Override
protected void onResume() {
super.onResume();
videoView.start();
}
And add
#Override
protected void onStop() {
super.onPause();
videoView.stopPlayback();
}
#Override
protected void onRestart() {
super.onResume();
videoView.start();
}
Because onPause will be called when your window is even partially obscured, like a pop up appearing.
Edit: Since you asked me to explain why it works you need to understand the activty life cycle. OnStart() will be called when the activity is completly not visible to the user, but onPause will be called when the activity is even partially invisible, like an alert box showing on the activity, or when you pull down to see your notifications etc.
To know more about activity life cycle check this out
Use
onStop() instead of onPause()
and
onRestart() instead of onResume()
Good Luck...!

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.

Activity com.exe.sample has leaked IntentReceiver android.media.MediaPlayer$1#4123c640

I have two activities in my android application.I am playing video in video view in both the activities.When i move to second activity its started playing video on videoview. But when I return back to previous activity and try to interact with UI, I am getting:
java.lang.IllegalArgumentException: Receiver not registered:
android.media.MediaPlayer$1#4123c640
And also I am getting:
Activity com.exe.sample.Test1 has leaked IntentReceiver android.media.MediaPlayer$1#4123c640 that was originally registered here. Are you missing a call to unregisterReceiver()?
in Second activity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
Button b=(Button)findViewById(R.id.button1);
VideoView videoview = (VideoView) findViewById(R.id.videoView1);
videoview.setVideoURI(Uri.parse(url));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, Test1.class));
}
});
}
Second Activity code:
public class Test1 extends Activity { private VideoView videoview;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tset);
videoview = (VideoView) findViewById(R.id.videoView2);
videoview
.setVideoURI(Uri
.parse(url));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
super.onCreate(savedInstanceState);
}
add mp.release() in Test1.onStop().
the log show that 'android.media.MediaPlayer$1#4123c640' is a anonymous inner BroadcastReceiver class of MediaPlayer, it should be unregistered in MediaPlayer.release().

Playing video from the pause state in VideoView

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

Resume playing VideoView from onResume

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!

Categories

Resources