我是Android / Java的新手,并且在将应用程序置于后台之前一直在努力从左侧位置播放视频。我已经阅读了一些概述覆盖onPause和onResume的帖子,但不幸的是我似乎无法正常工作。现在所有视频都播放得很好,除非我将应用程序放入BG并再次打开它,视频从开始开始,而不是从剩下的地方恢复。这是我的代码,请帮忙。
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;
public class VideoPlayback extends Activity {
private static final String TAG = Dashboard.class.getSimpleName();
ProgressBar progressBar = null;
VideoView vidView;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_playback);
Intent intent = getIntent();
String message = intent.getStringExtra(Dashboard.EXTRA_MESSAGE);
String link = "http://" + message;
vidView = (VideoView) findViewById(R.id.myVideo);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
Uri vidUri = Uri.parse(link);
Log.d(TAG, link);
vidView.setVideoURI(vidUri);
try {
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
vidView.requestFocus();
vidView.start();
progressBar.setVisibility(vidView.VISIBLE);
vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1, int arg2) {
// TODO Auto-generated method stub
Log.d(TAG, "Changed");
progressBar.setVisibility(vidView.GONE);
mp.start();
}
});
}
});
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(String.valueOf(event.getKeyCode())=="4"){
finish();
Log.v("key pressed", String.valueOf(event.getKeyCode()));
}
return super.dispatchKeyEvent(event);
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG, "PAUSE");
vidView.pause();
pos=vidView.getCurrentPosition();
Log.d("POS",String.valueOf(pos));
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Resume");
Log.d("R_POS", String.valueOf(pos));
vidView.seekTo(pos);
//vidView.resume();
}
}
答案 0 :(得分:0)
好的,所以我最终得到了这个,其中一个问题是当应用程序在后台运行时,暂停时的视频位置会丢失,因此我使用onSaveInstanceState来保留值,其次我正在流式传输的视频是{{ 1}}但不可寻求。一旦我开始使用.mp4
编码,一切都落入到位。这是我的代码:
H.264