The surface has been released when I try to setDisplay to MediaPlayer

后端 未结 6 630
忘掉有多难
忘掉有多难 2021-01-03 22:23

My xml file:



        
6条回答
  •  花落未央
    2021-01-03 23:11

    It's something related to the sequence of executing, as the surface has to be created first before setting display for the MediaPlayer, so you have to override the callback method surfaceCreated to the following:

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mp.setDisplay(sh); // now "mp" is defined as a class variable
    }
    

    and now there is no need to setDisplay inside your play method:

    private MediaPlayer mp; // to use it inside surfaceCreated callback method
    public void playVideo() {
        mp = new MediaPlayer();
        SurfaceView sv = (SurfaceView) this.findViewById(R.id.surfaceView);
        try {
            mp.setDataSource("sdcard/test/a.3gp");
            SurfaceHolder sh = sv.getHolder();
            mp.prepare();
            mp.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题