The surface has been released error causes (pro-test validity)

Keywords: MediaPlayer SurfaceView

Looking at a lot of information from the Internet, I found that many of them are wrong, so I intentionally updated the reasons for this problem today.
When playing mediaPlayer.start(), SurfaceHolder did not come and called onCreate() method to create view, which resulted in the holder not loading successfully, because the mediaplayer was played on SurfaceHolder, so the playback exception was thrown. Posting the correct code

 mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
        AssetManager assetManager = getAssets();
        try {
            mDescriptor = assetManager.openFd("test1.mp4");
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer = new MediaPlayer();
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.d(TAG, "surfaceCreated=" + System.currentTimeMillis());
                try {
                    mediaPlayer.setDataSource(mDescriptor.getFileDescriptor(), mDescriptor.getStartOffset(), mDescriptor.getLength());

                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//Setting Play Sound Type
                    mediaPlayer.prepare();
                    mediaPlayer.setDisplay(mSurfaceView.getHolder());

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.d(TAG, "surfaceChanged=" + System.currentTimeMillis());
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.d(TAG, "surfaceDestroyed=" + System.currentTimeMillis());
            }
        });


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG, "onPrepared=" + System.currentTimeMillis());
                mediaPlayer.start();
            }
        });


        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, " times onCompletion=" + System.currentTimeMillis());
                mediaPlayer.release();
            }
        });
    }

The life cycle of media player broadcasting:

Surface Created --- Surface Holder is created to avoid The surface has been released problem as long as it is set up when Surface Holder is created.
Surface Changed - Surface Holder changed
onPrepared - mediaPlayer prepares ok
onCompletion --- Media Player Playback Completion
Surface Destroyed - Surface Holder was destroyed.
If you have any questions, please point them out. Thank you.

Posted by mirana on Wed, 06 Feb 2019 13:36:17 -0800