我正在儿子的平板电脑上写一部电影应用程序,并定期点击后面或主页按钮。 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();
}
}
答案 0 :(得分:3)
我看到您将stopPosition
存储在onStop()
中,并在stopPosition != 0
时恢复 - 但是stopPosition
在启动之间没有保留。当用户按下后退按钮时,默认情况下您的活动已完成,这意味着下次启动该应用时,将无法维护您设置的任何状态。
要解决此问题,您可以使用SharedPreferences
来保留stopPosition
。例如:
private static final String KEY_POSITION = "KEY_POSITION";
@Override
public void onStop(){
super.onStop();
stopPosition = videoview.getCurrentPosition();
videoview.stopPlayback();
persistStopPosition(stopPosition);
}
private void persistStopPosition(int stopPosition){
SharedPreferences.Editor preferences = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
preferences.putInt(KEY_POSITION, stopPosition);
preferences.commit(); // need this to ensure data is persisted.
}
然后在onStart()
:
@Override
public void onStart(){
SharedPreferences preferences = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
stopPosition = preferences.getInt(KEY_POSITION, -1);
if (stopPosition != -1) {
videoview.seekTo(stopPosition);
}
videoview.start();
super.onStart();
}
答案 1 :(得分:2)
创建自己的MediaController
MyMediaController.java
public class MyMediaController extends MediaController {
public MyMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
public MyMediaController(Context context) {
super(context);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
((Activity) getContext()).finish();
return true;
}
return super.dispatchKeyEvent(event);
}}
然后在你的onCreate活动中。 VideoActivity.java
video=(VideoView)findViewById(R.id.vvVideo);
MyMediaController mediaController = new MyMediaController(this);
video.setMediaController(new MediaController(this){
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
finish();
return super.dispatchKeyEvent(event);
}
});
video.setMediaController(mediaController);