实施视频录制功能后,媒体录制器在自定义相机应用程序中失败

时间:2013-08-30 13:14:59

标签: java android camera android-camera video-capture

您好我正在开发一个Android相机应用程序,我正在实现视频录制功能。我正在使用切换按钮从相机切换到视频。我使用以下代码:

    private void prepareRecorder() 
    {
              recorder = new MediaRecorder();
              recorder.setPreviewDisplay(holder.getSurface());
              if (usecamera) 
              {
                camera.unlock();
                recorder.setCamera(camera);
              }
              recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
              recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
              recorder.setProfile(camcorderProfile);
      }


      public void surfaceCreated(SurfaceHolder holder)
       {
            if (usecamera) 
            {
            camera = Camera.open();
           try
           {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
           }
        catch (IOException e)
        {
            Log.e(LOGTAG,e.getMessage());
            e.printStackTrace();
            releaseCamera();
        }   
    }
}

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
      {
    Log.v(LOGTAG, "surfaceChanged");

    if (!recording && usecamera)
    {
        if (previewRunning)
        {
            camera.stopPreview();
        }

        try 
        {
            Parameters p = camera.getParameters();
            p.setPreviewSize(camcorderProfile.videoFrameWidth,camcorderProfile.videoFrameHeight);
            p.setPreviewFrameRate(camcorderProfile.videoFrameRate);
            camera.setParameters(p);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }   
        prepareRecorder();  
    }

}
         public void surfaceDestroyed(SurfaceHolder holder)
          { 
                   if (recording) 
               {
                  recorder.stop();
                  recording = false;
                 }
                  recorder.release();
                 if (usecamera)
                 {
                previewRunning = false;
        camera.lock();
        camera.release();
    }

}

但问题是,当我从相机切换到视频时,相机预览会持续几秒钟并返回相机的主预览。我收到以下错误:

        08-30 17:52:27.843: E/MediaRecorder(6421): prepare failed: -15

没有得到我错的地方。请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

            camera.unlock();

    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mediaRecorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
    mediaRecorder.setMaxDuration((int) preVideoLenght);

    try {
        mediaRecorder.prepare();
        mediaRecorder.start();

        infoText.setText(R.string.recording);
        return true;

    } catch (Exception e) {
        return false;
    }