2012-05-08 177 views
6

我试图在Android上创建一个录像机,并且我已经准备好了应该工作的代码 - 但我经常收到一条错误消息start failed: -19Android MediaRecorder - “启动失败:-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: -19捕获了RuntimeException。有类似question,但这并不能解决我的问题。

是否有任何其他原因得到这样的错误?

回答

14

刚刚发现的bug,在这条线:

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

注释掉这一行后,代码运行完美!

+0

不工作的三星盛大二重奏 – Neji

+4

谈到这一行出将引发强制关闭三星设备,如GN2,GS。这不是一个解决方案。虽然我还没有开始android,但现在我有点不舒服了!他们只是让制造商自由地制造没有标准的手机。 – Dante

+2

如果您删除此行,您如何指定视频大小? – TOP

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

有关详细指南请参阅本Open Source Cuxtom Cam

0

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

,为什么这个代码错误...

从研究,我都做了,错误代码-19谈到关于由MediaRecorder#setVideoSize()

来看,这种设置时,有与视频大小的问题代码,看看whitch屏幕,您在您的设备摄像头可以支持:

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

我有一些特定的手机的问题,我已经发现,我不能在其中一些设置camcoder配置文件的大小。但是,当它为有问题的机器人工作,它停止工作在以前的工作设备。

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

  • 设置宽度/高度
  • 尝试启动merdia记录
  • 在例外的情况下,不设置宽度/高度
  • 再试一次

一种垃圾逻辑,但工作。

我设置与实施github上项目,尝试一下:https://github.com/rafaelsilverio/MediaRecorder

相关问题