Android中的MediaPlayer IllegalStateException

时间:2016-08-26 08:17:08

标签: android android-mediaplayer surfaceview illegalstateexception

我有SurfaceView,我从sdcard播放视频。当视频播放完毕后,我尝试显示图像5秒,然后再播放我的视频。当我运行我的应用程序并且视频已经完成时,我有IllegalStateException

 videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
    videoHolder = videoSurface.getHolder();
    videoHolder.addCallback(this);

    player = new MediaPlayer();
    controller = new VideoControllerView(this);
    player.setOnPreparedListener(this);


    try {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        String filePath = Environment.getExternalStorageDirectory() + "/myvideo/Sample.mp4";
        player.setDataSource(filePath);
        player.setOnPreparedListener(this);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoSurface.setVisibility(View.INVISIBLE);
                findViewById(R.id.imageView).setVisibility(View.VISIBLE);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                findViewById(R.id.imageView).setVisibility(View.INVISIBLE);
                                videoSurface.setVisibility(View.VISIBLE);

                            }
                        });
                        player.start();

                    }
                }, 5000);

            }
        });
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}



@Override
public boolean onTouchEvent(MotionEvent event) {
    controller.show();
    return false;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    player.setDisplay(holder);
    player.prepareAsync();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
@Override
public void onPrepared(MediaPlayer mp) {
    controller.setMediaPlayer(this);
    controller.setAnchorView((RelativeLayout) findViewById(R.id.videoSurfaceContainer));
    player.start();
}

@Override
public boolean canPause() {
    return true;
}

@Override
public boolean canSeekBackward() {
    return true;
}

@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getBufferPercentage() {
    return 0;
}

@Override
public int getCurrentPosition() {
    return player.getCurrentPosition();
}

@Override
public int getDuration() {
    return player.getDuration();
}

@Override
public boolean isPlaying() {
    return player.isPlaying();
}

@Override
public void pause() {
    player.pause();
}

@Override
public void seekTo(int i) {
    player.seekTo(i);
}

@Override
public void start() {
    player.start();
}

@Override
public boolean isFullScreen() {
    return false;
}

@Override
public void toggleFullScreen() {

}

也许问题在于这一行

videoSurface.setVisibility(View.VISIBLE);

我的代码中我做错了什么?

1 个答案:

答案 0 :(得分:0)

string

之前player.release()尝试onComplation