0

我正在我儿子的平板电脑上写一个电影应用程序,他会定期点击返回或主页按钮。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();
}
}
4

2 回答 2

3

我看到您正在存储stopPositioninonStop()并恢复如果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();
}
于 2013-10-21T01:43:15.217 回答
2

创建您自己的媒体控制器

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 中的活动中。视频活动.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);
于 2015-01-19T07:49:53.663 回答