The surface has been released 异常

http://dingran.iteye.com/blog/1442502

=====================================

在使用surfaceView和MediaPlayer播放视频的时候出现了一个异常,纠结了很久,经过同事帮助,终于发现了问题的根源。

 

异常如下:

 

E/AndroidRuntime( 1765): Caused by: java.lang.IllegalArgumentException: The surface has been released
E/AndroidRuntime( 1765):     at android.media.MediaPlayer._setVideoSurface(Native Method)
E/AndroidRuntime( 1765):     at android.media.MediaPlayer.setDisplay(MediaPlayer.java:633)

 

 

原因是在surfaceHolder还没准备好的时候,我就调用了MediaPlayer的start()方法。

 

正确的播放流程应该是:

 

        surfaceView = (SurfaceView) findViewById(R.id.PlaySurfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

 

      mediaPlayer.setDisplay(surfaceHolder);

 

try {
            
            mediaPlayer.setDataSource(this, Uri.parse("/sdcard/bianxing.ts"));
            
        } catch (IllegalArgumentException e1) {
            e1.printStackTrace();
        } catch (SecurityException e1) {
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            
            @Override
            public void onPrepared(MediaPlayer mp) {
                
                mediaPlayer.start();
                
            }
        });

 

这样就可以正常播放了,唉,不容易啊,感谢同事的帮助。

 

其实对Medialayer的封装来说,VideoView已经做的很好了,自己再写这些确实费力,期望以后扩展会好点


posted @ 2012-07-05 20:16  夏至冬末  阅读(2844)  评论(0编辑  收藏  举报