我正在我儿子的平板电脑上写一个电影应用程序,他会定期点击返回或主页按钮。2 岁时,他因为丢失了正在观看的视频而感到沮丧。当他点击主页按钮时,我能够保存状态,因此当他返回应用程序时,电影会继续播放。不幸的是,无论我如何尝试设置后退按钮,它总是从头开始。
我已经尝试过 onBackPressed 和 KeyDown 方法,但它们似乎确实有效。我错过了什么吗?
感谢任何帮助,如果这是重复的并且我没有找到它,我深表歉意。
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 <String> videoPaths = new ArrayList<String>();
@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 <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 >= 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()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(MainActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
i = (i + 1);
if (i >= 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() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
i = (i - 1);
if (i < 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();
}
}