8

我正在开发视频录制应用程序,我想在其中显示预览,当用户单击录制按钮时它开始录制,当用户单击停止按钮时它停止录制。

我在我的表面上获得了视频预览,但是当我按下开始按钮时,它崩溃并出现错误“MEDIA.RECORDER.START(Native MEthod)。这是我的代码请帮助我。

  @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            Button start =(Button)findViewById(R.id.start);
            Button stop =(Button)findViewById(R.id.stop);
            stop.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    camera.stopPreview();
                    stopRecording();
                }
            });
            start.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startRecording();
                }
            });
    }

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    if (camera != null){
        Camera.Parameters params = camera.getParameters();
        camera.setParameters(params);
    }
    else {
        Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if (previewRunning){
        camera.stopPreview();
    }
    Camera.Parameters p = camera.getParameters();
     List<Camera.Size> sizes = p.getSupportedPreviewSizes(); 
     Camera.Size cs = sizes.get(0);  
     p.setPreviewSize(cs.width, cs.height);  
       camera.setParameters(p);

    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        previewRunning = true;
    }
    catch (IOException e) {
        Log.e(TAG,e.getMessage());
        e.printStackTrace();
    }
}

 private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final long maxFileSizeInBytes = 500000;
    private final int videoFramesPerSecond = 20;

    public boolean startRecording(){
        try {
            camera.unlock();

            mediaRecorder = new MediaRecorder();

            mediaRecorder.setCamera(camera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

            mediaRecorder.setMaxDuration(maxDurationInMs);

        File tempFile = new File(getCacheDir(),"test.mp4");
            mediaRecorder.setOutputFile(tempFile.getPath());

            mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
            mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

            mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

                        mediaRecorder.prepare();
            mediaRecorder.start();

            return true;
        } catch (IllegalStateException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public void stopRecording(){
        mediaRecorder.stop();
        camera.lock();
    }

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    previewRunning = false;
    camera.release();
}

}

日志是

 08-31 02:20:11.781: E/MediaRecorder(14519): start failed: -19
 08-31 02:20:11.781: D/AndroidRuntime(14519): Shutting down VM
 08-31 02:20:11.781: W/dalvikvm(14519): threadid=1: thread exiting with uncaught exception (group=0x416c9700)
 08-31 02:20:11.781: E/AndroidRuntime(14519): FATAL EXCEPTION: main
 08-31 02:20:11.781: E/AndroidRuntime(14519): java.lang.RuntimeException: start failed.
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.media.MediaRecorder.start(Native Method)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at com.example.cameratest.MainActivity.startRecording(MainActivity.java:135)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at com.example.cameratest.MainActivity$2.onClick(MainActivity.java:61)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.view.View.performClick(View.java:4240)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.view.View$PerformClick.run(View.java:17721)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Handler.handleCallback(Handler.java:730)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Handler.dispatchMessage(Handler.java:92)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Looper.loop(Looper.java:137)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.app.ActivityThread.main(ActivityThread.java:5103)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at dalvik.system.NativeStart.main(Native Method)
4

6 回答 6

12

我找到了这个解决方案,它对我来说工作正常并解决了这个问题:)

对于 2.3+ 安卓:

只是改变:

mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

至:

mediaRecorder.setProfile(CamcorderProfile.get(CameraID,CamcorderProfile.QUALITY_HIGH));
于 2013-12-04T23:19:29.180 回答
3

在您的 startRecording 方法中尝试在解锁之前锁定相机:

mediaRecorder = new MediaRecorder();
camera.lock();
camera.unlock();
于 2013-08-28T06:15:59.827 回答
2

注释这行代码,

mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());
于 2015-07-21T04:43:48.733 回答
0

尝试以下可能会起作用:对任何人都有帮助:

        try {
            mediaRecorder.prepare();
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        mediaRecorder.start();

来电需要时间来准备。

保持准备1秒钟,一切正常。

于 2014-09-22T10:01:25.167 回答
0

我通过将 videoFramesPerSecond 值更改为 30 解决了启动失败错误 -19。这是与 1080p (1920 x 1080) 分辨率相对应的 QUALITY_1080p-质量级别和与 480p (720 x 480) 分辨率相对应的 QUALITY_480P-质量级别的推荐值) 解析度。

来源:http: //developer.android.com/guide/appendix/media-formats.html

于 2013-09-26T17:23:44.687 回答
-3

这可能没有你想象的那么复杂。Android 已经为使用Intents录制和返回视频文件等简单的事情做了很多工作......

使用相机应用程序录制视频 Android 将操作委托给其他应用程序的方式是调用描述您想要完成的操作的 Intent。这涉及到三个部分:Intent 本身、启动外部 Activity 的调用,以及当焦点返回到您的 Activity 时处理视频的一些代码。

这是一个调用意图来捕获视频的函数。

private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
}

查看视频 Android Camera 应用程序将传递给 onActivityResult() 的 Intent 中的视频作为 Uri 返回,该 Uri 指向存储中的视频位置。以下代码检索此视频并将其显示在 VideoView 中。

private void handleCameraVideo(Intent intent) {
mVideoUri = intent.getData();
mVideoView.setVideoURI(mVideoUri);
}

将此添加到清单

<uses-feature android:name="android.hardware.camera" />

来源:http: //developer.android.com/training/camera/videobasics.html

于 2013-08-28T08:18:38.980 回答