Android MediaRecorder - “start failed: -19”

后端 未结 5 1370
广开言路
广开言路 2020-11-27 21:42

I\'m trying to create a video recorder on Android, and I\'ve prepared my code which is supposed to be working - but I constantly get an error message start failed: -19

5条回答
  •  没有蜡笔的小新
    2020-11-27 22:40

    I solved my problem once i added this for video recording

    /**
     * Start video recording by cleaning the old camera preview
     */
    private void startVideoRecorder() {
        // THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF
        // "MediaRecorder start failed: -19"
        // THIS WONT BE NEEDED INCASE OF PHONE AND TABLET
        // This causes crash in glass kitkat version so remove it
        // try {
        // mCamera.setPreviewDisplay(null);
        // } catch (java.io.IOException ioe) {
        // Log.d(TAG,
        // "IOException nullifying preview display: "
        // + ioe.getMessage());
        // }
        // mCamera.stopPreview();
        // mCamera.unlock();
        recorder = new MediaRecorder();
        // Let's initRecorder so we can record again
        initRecorder();
    }
    
    /**
     * Initialize video recorder to record video
     */
    private void initRecorder() {
        try {
            File dir = new File(folderPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            mCamera.stopPreview();
            mCamera.unlock();
            videofile = new File(dir, fileName + ".mp4");
            recorder.setCamera(mCamera);
    
            // Step 2: Set sources
            recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
            // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
            recorder.setProfile(CamcorderProfile
                    .get(CamcorderProfile.QUALITY_HIGH));
    
            // Step 4: Set output file
            recorder.setOutputFile(videofile.getAbsolutePath());
            // Step 5: Set the preview output
            recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
            // Step 6: Prepare configured MediaRecorder
            recorder.setMaxDuration(video_duration * 1000);
            recorder.setOnInfoListener(new OnInfoListener() {
    
                @Override
                public void onInfo(MediaRecorder mr, int what, int extra) {
                    if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
    
                        mCamera.stopPreview();
                        releaseMediaRecorder();
    
                        /*
                         * initiate media scan and put the new things into the
                         * path array to make the scanner aware of the location
                         * and the files you want to see
                         */MediaScannerConnection.scanFile(
                                CuxtomCamActivity.this,
                                new String[] { videofile.getPath() }, null,
                                null);
    
                        Intent intent = new Intent();
                        intent.putExtra(CuxtomIntent.FILE_PATH,
                                videofile.getPath());
                        intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
    
                }
            });
            recorder.prepare();
            recorder.start();
        } catch (Exception e) {
            Log.e("Error Stating CuXtom Camera", e.getMessage());
        }
    }
    private void releaseMediaRecorder() {
        if (recorder != null) {
            recorder.reset(); // clear recorder configuration
            recorder.release(); // release the recorder object
            recorder = null;
        }
    }
    

    For detailed guide refer to this Open Source Cuxtom Cam

提交回复
热议问题