Android MediaRecorder - "start failed: -19"

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()都已正确执行。然后它捕获了一个带有消息start failed: -19RuntimeException。有一个类似的问题,但这并没有解决我的问题。
还有其他原因会导致这样的错误吗?
5个回答

16

刚刚发现了一个bug,出现在这一行代码中:

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

注释掉这行代码后,程序可以完美运行!


5
将此行注释掉会导致三星设备(如GN2、GS)强制关闭,这不是解决方案。虽然我开始接触Android还不久,但现在我有点厌烦Android了!他们只是让制造商自由制作手机,而没有标准化。 - Dante
3
如果删除这行代码,你如何指定视频大小? - TOP
@TOP 我认为你应该检查支持的相机分辨率,然后使用它。并非每个视频大小都是有效的。 - itzhar
我没有设置mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight()); 但它仍然显示相同的错误。 - Megha Maniar

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

想要获得详细的指南,请参考这个开源 Cuxtom Cam


0

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

为什么会出现这个错误呢...

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

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

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();
        }
    }

0

我曾经遇到过一些特定手机的问题,发现在其中一些手机上无法设置摄像头配置大小。但是当这个问题在有问题的安卓手机上解决后,之前正常工作的设备也出现了同样的问题。

所以最终我的实现逻辑是:

  • 设置宽度/高度
  • 尝试启动媒体录制器
  • 如果出现异常,请不要再次设置宽度/高度重试

这种逻辑有点儿糟糕,但是确实有效。

我已经在Github上建立了一个项目,你可以试试:https://github.com/rafaelsilverio/MediaRecorder


0

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

MediaRecorder .setVideoSize()
MediaRecorder .setVideoFrameRate()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接