Android:MediaRecorder启动失败:-16

时间:2013-04-19 20:17:04

标签: android

我一直在尝试实施摄像机。但由于在record.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):prepare:surface = 0x257468(identity = 1459)   04-20 18:27:27.548:E / MediaRecorder(13225):开始失败:-16   04-20 18:27:27.558:D / AndroidRuntime(13225):关闭虚拟机   04-20 18:27:27.558:W / dalvikvm(13225):threadid = 1:线程退出未捕获异常(组= 0x40018560)   04-20 18:27:27.578:E / AndroidRuntime(13225):致命异常:主要   04-20 18:27:27.578:E / AndroidRuntime(13225):java.lang.RuntimeException:start failed。   04-20 18:27:27.578:E / AndroidRuntime(13225):在android.media.MediaRecorder.start(Native Method)   04-20 18:27:27.578:E / AndroidRuntime(13225):at 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:E / AndroidRuntime(13225):在android.view.View $ PerformClick.run(View.java:9116)   04-20 18:27:27.578:E / AndroidRuntime(13225):在android.os.Handler.handleCallback(Handler.java:587)   04-20 18:27:27.578:E / AndroidRuntime(13225):在android.os.Handler.dispatchMessage(Handler.java:92)   04-20 18:27:27.578:E / AndroidRuntime(13225):在android.os.Looper.loop(Looper.java:130)   04-20 18:27:27.578:E / AndroidRuntime(13225):在android.app.ActivityThread.main(ActivityThread.java:3835)   04-20 18:27:27.578:E / AndroidRuntime(13225):at java.lang.reflect.Method.invokeNative(Native Method)   04-20 18:27:27.578:E / AndroidRuntime(13225):at java.lang.reflect.Method.invoke(Method.java:507)   04-20 18:27:27.578:E / AndroidRuntime(13225):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:864)   04-20 18:27:27.578:E / AndroidRuntime(13225):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)   04-20 18:27:27.578:E / AndroidRuntime(13225):at dalvik.system.NativeStart.main(Native Method)

我知道有一些重复,但没有一个答案对我有用。请帮帮我。在此先感谢

0 个答案:

没有答案
相关问题