我尝试了很多方法,对代码进行了很多更改,阅读了Android MediaPlayer文档,尝试了StackOverflow示例,但没有一个可以解决我的问题。
我的问题:当我按模拟器或电话的主页按钮,然后重新打开应用程序时,它是从头开始的。
希望您能够帮助我。提前致谢。
这是我的代码:
public class Mediaplayer extends Activity implements OnCompletionListener,
OnErrorListener, OnInfoListener, OnPreparedListener,
OnSeekCompleteListener, OnVideoSizeChangedListener,
SurfaceHolder.Callback, MediaController.MediaPlayerControl {
Display currentDisplay;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
MediaController controller;
int videoWidth = 0;
int videoHeight = 0;
boolean readyToPlay = false;
public final static String LOGTAG = "CUSTOM_VIDEO_PLAYER";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
String filePath = "http://cdn.example.com/wp-content/uploads/Almost.Human-S01E10.mp4";
try {
mediaPlayer.setDataSource(filePath);
} catch (IllegalArgumentException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
Log.v(LOGTAG, e.getMessage());
finish();
}
controller = new MediaController(this);
currentDisplay = getWindowManager().getDefaultDisplay();
}
public void surfaceCreated(SurfaceHolder holder) {
// Log.v(LOGTAG, "surfaceCreated Called");
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
// Log.v(LOGTAG, e.getMessage());
finish();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.v(LOGTAG, "surfaceChanged Called");
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v(LOGTAG, "surfaceDestroyed Called");
}
public void onCompletion(MediaPlayer mp) {
Log.v(LOGTAG, "onCompletion Called");
finish();
}
public boolean onError(MediaPlayer mp, int whatError, int extra) {
Log.v(LOGTAG, "onError Called");
if (whatError == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
Log.v(LOGTAG, "Media Error, Server Died " + extra);
} else if (whatError == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
Log.v(LOGTAG, "Media Error, Error Unknown " + extra);
}
return false;
}
public boolean onInfo(MediaPlayer mp, int whatInfo, int extra) {
if (whatInfo == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) {
Log.v(LOGTAG, "Media Info, Media Info Bad Interleaving " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) {
Log.v(LOGTAG, "Media Info, Media Info Not Seekable " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_UNKNOWN) {
Log.v(LOGTAG, "Media Info, Media Info Unknown " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Log.v(LOGTAG, "MediaInfo, Media Info Video Track Lagging " + extra);
/*
* Android Version 2.0 and Higher } else if (whatInfo ==
* MediaPlayer.MEDIA_INFO_METADATA_UPDATE) {
* Log.v(LOGTAG,"MediaInfo, Media Info Metadata Update " + extra);
*/
}
return false;
}
public void onPrepared(MediaPlayer mp) {
Log.v(LOGTAG, "onPrepared Called");
videoWidth = mp.getVideoWidth();
videoHeight = mp.getVideoHeight();
if (videoWidth > currentDisplay.getWidth()
|| videoHeight > currentDisplay.getHeight()) {
float heightRatio = (float) videoHeight
/ (float) currentDisplay.getHeight();
float widthRatio = (float) videoWidth
/ (float) currentDisplay.getWidth();
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
videoHeight = (int) Math.ceil((float) videoHeight
/ (float) heightRatio);
videoWidth = (int) Math.ceil((float) videoWidth
/ (float) heightRatio);
} else {
videoHeight = (int) Math.ceil((float) videoHeight
/ (float) widthRatio);
videoWidth = (int) Math.ceil((float) videoWidth
/ (float) widthRatio);
}
}
}
surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
videoHeight));
mp.start();
controller.setMediaPlayer(this);
controller.setAnchorView(this.findViewById(R.id.MainView));
controller.setEnabled(true);
controller.show();
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
return;
}
}
public void onSeekComplete(MediaPlayer mp) {
Log.v(LOGTAG, "onSeekComplete Called");
}
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.v(LOGTAG, "onVideoSizeChanged Called");
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
public int getBufferPercentage() {
return 0;
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public void pause() {
mediaPlayer.pause();
}
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
public void start() {
mediaPlayer.start();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
controller.show();
return false;
}
@Override
public int getAudioSessionId() {
// TODO Auto-generated method stub
return 0;
}
}
我了解您已经接受了答案,但是我将根据您对问题的评论以及您和Nana对Nana答案的评论来解释问题所在。
SurfaceView
当A不再可见时将销毁,并在再次变得可见时将其重新创建。mp.pause()
您的Activity
onPause()
方法。SurfaceView
重新创建。surfaceCreated
方法中,您正在调用,mp.prepare()
但此时mp
处于暂停状态,调用prepare()
暂停Mediaplayer
将抛出IllegalStateException
。try / catch
可以捕获IllegalStateException
并调用的块finish()
-这就是为什么第一次尝试从“最近”列表中重新启动该应用程序会导致Activity
损坏的原因。Activity
已被销毁,因此将通过一个完整的创建过程(onCreate(..)
称为)。这就是为什么从开始。Nana的答案是一种解决方法,但这仍然意味着必须两次尝试Activity
从“最近”列表中重新启动您。
不幸的是,MediaPlayer
该类缺少用于检查“状态”的方法,isPlaying
而该方法是任何状态的唯一有用方法。类开发人员没有想到添加一种getState()
方法(或类似方法)来检查其是否已启动,正在播放,正在播放,已停止,已暂停等情况,这真是令人遗憾。
防止这种IllegalStateException
情况发生的一种方法是isPaused
(例如)设置一个布尔值,然后Activity
onPause()
按如下所示修改您的...
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
isPaused = true;
}
...并在surfaceCreated(...)
...
try {
if (isPaused) {
mpStart();
isPaused = false;
}
else
mediaPlayer.prepare();
}
// catch blocks here
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句