1

我一直在尝试实现摄像机。但由于 recorder.start() 上的这个神秘错误 (-16),它从未起作用。

这是我的代码:

private void initRecorder(int width, int height) {
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        Parameters params = camera.getParameters();
        List<Size> sizes = params.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, width, height);
        params.setPreviewSize(optimalSize.width, optimalSize.height);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_LOW);
        recorder.setProfile(cpHigh);
        File mediaFile = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            mediaFile = new File(
                    android.os.Environment.getExternalStorageDirectory()
                            + "/Towncare/Temp/vid_temp.3gpp");
            if (!mediaFile.exists()) {
                try {
                    mediaFile.getParentFile().mkdirs();
                    mediaFile.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        ;
        recorder.setOutputFile(android.os.Environment
                .getExternalStorageDirectory() + "/Towncare/Temp/vid_temp.3gpp");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(5000000);
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(previewHolder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

这是日志:

04-20 18:27:26.178:I/MediaRecorderJNI(13225):准备:表面=0x257468(身份=1459) 04-20 18:27:27.548:E/MediaRecorder(13225):启动失败:-16 04-20 18:27:27.558: D/AndroidRuntime(13225): 关闭 VM 04-20 18:27:27.558: W/dalvikvm(13225): threadid=1: 线程退出但未捕获异常 (group=0x40018560) 04-20 18 :27:27.578:E / AndroidRuntime(13225):致命异常:主要04-20 18:27:27.578:E / AndroidRuntime(13225):java.lang.RuntimeException:启动失败。04-20 18:27:27.578: E/AndroidRuntime(13225): 在 android.media.MediaRecorder.start(Native Method) 04-20 18:27:27.578: E/AndroidRuntime(13225): 在 com.packagename.app .FullCameraActivity$3.onClick(FullCameraActivity.java:84) 04-20 18:27:27.578: E/AndroidRuntime(13225): 在 android.view.View.performClick(View.java:2506) 04-20 18:27: 27.578:

我知道有一些重复,但它们都没有对我有用的答案。请帮帮我。提前致谢

4

0 回答 0