Note that there are some explanatory texts on larger screens.

plurals
  1. POResuming video after back button was pressed
    text
    copied!<p>I am writing an app for movies on my son's tablet and periodically he hits the back or home button. Being 2 yrs old he gets frustrated because he lost the video he was watching. I am able to save the state when he hits the home button so when he goes back into the app the movie resumes. Unfortunately, no matter how I try to set the back button, it always starts at the beginning again. </p> <p>I have tried the onBackPressed and KeyDown Methods, but they done seem to work. Am I missing something?</p> <p>I appreciate any help and I apologize if this is a duplicate and I not finding it.</p> <pre><code>package com.example.toddlerplayer; import java.io.File; import java.util.ArrayList; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import android.widget.VideoView; import com.example.scoobyplayer.R; public class MainActivity extends Activity implements OnGestureListener { private VideoView videoview; private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gDetector; int i = 0; int stopPosition = -1; String startFile = "startFile"; String startState = "startState"; String startArray; ArrayList &lt;String&gt; videoPaths = new ArrayList&lt;String&gt;(); @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); File dir = new File("/mnt/sdcard2/Movies/scooby/"); videoview = (VideoView)findViewById(R.id.myvideoview); videoview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); if (savedInstanceState != null) { videoPaths = savedInstanceState.getStringArrayList("movieList"); i = savedInstanceState.getInt(startFile); stopPosition = savedInstanceState.getInt(startState); } else { String [] chld = dir.list(); for (int x =0; x &lt;chld.length; x++) { String fname = chld[x]; videoPaths.add(dir+"/"+fname); } } videoview.setVideoPath(videoPaths.get(i)); //videoview.setOnCompletionListener(this); //videoview.setOnPreparedListener(this); //videoview.setOnTouchListener(this); // Gesture detection gDetector = new GestureDetector(this); videoview.start(); videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(final MediaPlayer mp) { i = (i + 1); if (i &gt;= videoPaths.size()) { finish(); } else { videoview.setVideoPath(videoPaths.get(i)); videoview.start(); } } }); } @Override public boolean onTouchEvent(MotionEvent me) { boolean result = gDetector.onTouchEvent(me); if(!result){ if(me.getAction() == MotionEvent.ACTION_DOWN){ if(videoview.isPlaying()){ videoview.pause(); result = true; } else { videoview.start(); result = false; } //result = true; } else { result = false; } } return result; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MAX_OFF_PATH) return false; // right to left swipe if(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { //Toast.makeText(MainActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show(); i = (i + 1); if (i &gt;= videoPaths.size()) { i = 0; } videoview.stopPlayback(); videoview.setVideoPath(videoPaths.get(i)); videoview.start(); Toast.makeText(MainActivity.this,videoPaths.get(i), Toast.LENGTH_SHORT).show(); //left to right swipe } else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { //Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show(); i = (i - 1); if (i &lt; 0) { i = videoPaths.size() - 1; } videoview.stopPlayback(); videoview.setVideoPath(videoPaths.get(i)); videoview.start(); Toast.makeText(MainActivity.this,videoPaths.get(i), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { // nothing } return false; } @Override public boolean onDown(MotionEvent arg0) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub return false; } @Override public void onStop() { super.onStop(); stopPosition = videoview.getCurrentPosition(); videoview.stopPlayback(); //Toast.makeText(MainActivity.this,stopPosition, Toast.LENGTH_SHORT).show(); } @Override public void onStart() { if (stopPosition != -1) { videoview.seekTo(stopPosition); } videoview.start(); super.onStart(); } public void onSaveInstanceState(Bundle outState) { outState.putInt(startState, stopPosition); outState.putInt(startFile, i); outState.putStringArrayList("movieList", videoPaths); super.onSaveInstanceState(outState); } @Override protected void onResume() { if (videoview != null) videoview.seekTo(stopPosition); videoview.resume(); super.onResume(); } @Override protected void onPause() { if (videoview != null) stopPosition = videoview.getCurrentPosition(); videoview.pause(); super.onPause(); } @Override public void onBackPressed() { stopPosition = videoview.getCurrentPosition(); videoview.pause(); super.onBackPressed(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload