7

我正在尝试在 Android 上创建一个录像机,并且我已经准备了应该可以工作的代码 - 但我经常收到一条错误消息start failed: -19

这是我的代码:

public boolean startRecording() {
    try {
        camera.unlock();
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {

                @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                Log.i(TAG, "Error");
            }
        });

        mediaRecorder.setCamera(camera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        Log.i(TAG, "a");

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        Log.i(TAG, "b");

        mediaRecorder.setMaxDuration(maxDurationInMs); // set to 20000

        String uniqueOutFile = OUTPUT_FILE + System.currentTimeMillis() + ".3gp";
        File outFile = new File(uniqueOutFile);
        if (outFile.exists()) {
            outFile.delete();
        }
        mediaRecorder.setOutputFile(uniqueOutFile);
        mediaRecorder.setVideoFrameRate(videoFramesPerSecond); // set to 20
        mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
        Log.i(TAG, "c");

        mediaRecorder.setPreviewDisplay(holder.getSurface());
        mediaRecorder.setMaxFileSize(maxFileSizeInBytes); // set to 50000
        mediaRecorder.prepare();
        Log.i(TAG, "d");

        mediaRecorder.start();
        Log.i(TAG, "e");

        return true;
        } catch (IllegalStateException e) {
            Log.i(TAG, "f");
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
            camera.lock();
            return false;
        } catch (IOException e) {
            Log.i(TAG, "g");
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
            camera.lock();
            return false;
        } catch (RuntimeException e) {
            Log.i(TAG, "h");
            Log.e(TAG, e.getMessage());
            camera.lock();
            return false;
        }
    }

所有调试日志(从“a”到“d”)都打印在日志中,因此似乎所有步骤mediaRecorder.prepare()都已正确完成。然后它会捕获一条RuntimeExceptionwith 消息start failed: -19。有一个类似的问题,但这并不能解决我的问题。

还有其他原因会出现这样的错误吗?

4

5 回答 5

16

刚刚发现了这个错误,在这一行:

mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());

注释掉这一行后,代码运行完美!

于 2012-05-08T11:39:54.473 回答
2

一旦我添加了这个用于视频录制,我就解决了我的问题

/**
 * 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;
    }
}

有关详细指南,请参阅此Open Source Cuxtom Cam

于 2014-05-21T12:46:51.180 回答
0

问题出在您的setVideoSize()代码中。

以及为什么此代码错误...

根据我所做的研究,错误代码 -19 出现在由设置的视频大小出现问题时MediaRecorder#setVideoSize()

运行此代码,并查看您的设备中的相机可以支持的屏幕:

final List<Camera.Size> mSupportedVideoSizes = getSupportedVideoSizes(mCamera);
        for (Camera.Size str : mSupportedVideoSizes)
            Log.e(TAG, "mSupportedVideoSizes "+str.width + ":" + str.height + " ... "
                    + ((float) str.width / str.height));

方法是:

public List<Size> getSupportedVideoSizes(Camera camera) {
        if (camera.getParameters().getSupportedVideoSizes() != null) {
            return camera.getParameters().getSupportedVideoSizes();
        } else {
            // Video sizes may be null, which indicates that all the supported 
            // preview sizes are supported for video recording.
            return camera.getParameters().getSupportedPreviewSizes();
        }
    }
于 2015-10-07T06:34:28.503 回答
0

我在某些特定手机上遇到了这个问题,我发现我无法在其中一些手机中设置 camcoder 配置文件大小。但是当这适用于有问题的机器人时,它停止在以前的工作设备上工作。

所以最后我实现的逻辑是这样的:

  • 设置宽度/高度
  • 尝试启动媒体录音机
  • 如果出现异常,请在不设置宽度/高度的情况下重试

有点垃圾逻辑,但那行得通。

我已经使用该实现设置了一个 github 项目,请尝试一下:https ://github.com/rafaelsilverio/MediaRecorder

于 2016-02-08T13:17:35.683 回答
0

我也遇到了这个问题,注释了以下两种方式,因为硬件不支持这两种配置。

MediaRecorder .setVideoSize()
MediaRecorder .setVideoFrameRate()
于 2018-03-21T05:19:45.987 回答