解决方法:
将start() 方法调用放到OnPreparedListener回调里。如:
mediaPlayer = new MediaPlayer();
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);
// 设置播放时打开屏幕
surfaceView.getHolder().setKeepScreenOn(true);
surfaceView.getHolder().addCallback(new SurfaceListener());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start(); //在onPreparedListener回调中调用start()方法
}
};
报错原因分析:
在调用start()
方法之前,需检查MediaPlayer的
状态,确保MediaPlayer
已经处于正确的状态。通常,您需要在调用prepare()
或prepareAsync()
之后,并且接收到OnPreparedListener
的回调之后再调用start()
导致报错-38的写法:
mediaPlayer = new MediaPlayer();
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);
// 设置播放时打开屏幕
surfaceView.getHolder().setKeepScreenOn(true);
surfaceView.getHolder().addCallback(new SurfaceListener());
mediaPlayer.start();