关于MediaPlayer+SurfaceView播放本地视频

如下代码报错prepareAsync called in state 1
java.lang.IllegalStateException
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1184)
at sangfei.android.com.audiotestapk.VideoMainActivity.startPlayFirstVideo(VideoMainActivity.java:165)
at sangfei.android.com.audiotestapk.VideoMainActivity.initFirstPlayer(VideoMainActivity.java:159)
at sangfei.android.com.audiotestapk.VideoMainActivity.surfaceCreated(VideoMainActivity.java:129)

`` @Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO 自动生成的方法存根

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    //surfaceView创建完毕后,首先获取该直播间所有视频分段的url

// getVideoUrls();
//然后初始化播放手段视频的player对象
initFirstPlayer();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO 自动生成的方法存根

}

/*
 * 初始化播放首段视频的player
 */
private void initFirstPlayer() {
    firstPlayer = new MediaPlayer();
    firstPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    firstPlayer.setDisplay(surfaceHolder);

    firstPlayer
            .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    onVideoPlayCompleted(mp);
                }
            });
    //设置cachePlayer为该player对象
    cachePlayer = firstPlayer;
    initNexttPlayer();
    //player对象初始化完成后,开启播放
    startPlayFirstVideo();
}

private void startPlayFirstVideo() {
    try {
        firstPlayer.setDataSource(String.valueOf(getList().get(currentVideoIndex)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        firstPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    firstPlayer.start();


}`


0

1个回答

是因为调用mediaPlayer.prepare()时,mediaPlayer为null导致的。建议你参考下:

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!