При использовании MediaPlayer и SurfaceView для воспроизведения видео. SurfaceCreated иногда вызывается, когда mMediaPlayer имеет значение null. Тогда метод "mMediaPlayer.setDisplay (holder)" в разделе "surfaceCreated (Surface holder)" не будет вызван.
После вызова mMediaPlayer.prepareAsync () mMediaPlayer выполнит .start () без установки surfaceHolder.
У меня нет способа наладить их отношения.
//pseudo code
public class PlayerManager extends BaseMediaPlayer implements SurfaceHolder.Callback,
MediaPlayer.OnCompletionListener,
MediaPlayer.OnPreparedListener {
private boolean isSurfaceCreated = false;
private boolean isMediaPlayerPrepared = false;
private boolean isMediaDataSourceOk = false;
private void initMediaPlayer(){
if(!null){
mMediaPlayer.release();
=null
}
new MediaPlayer();
isMediaPlayerPrepared = false;
isMediaDataSourceOk = false;
}
/**
* here!!!
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
isSurfaceCreated = true;
if (mMediaPlayer != null) {
mMediaPlayer.setDisplay(holder);
if (isMediaPlayerPrepared && isMediaDataSourceOk) {
play();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "surfaceDestroyed");
isSurfaceCreated = false;
}
/**
* here!!!
*/
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "MediaPlayer prepared");
isMediaPlayerPrepared = true;
if (isSurfaceCreated && isMediaDataSourceOk) {
play();
}
}
public void play(){
initMediaPlayer();
try mMediaPlayer.setDataSource(xxx);
catch e return;
isMediaDataSourceOk =true;
try mMediaPlayer.prepareAsync();
catch e return;
}
}