2014-11-21 55 views
2

我使用MediaRecorder在Android中录制视频,万亩当前PARAMATERS是:无法在Android的MediaRecorder设置手动录像大小

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
    mMediaRecorder.setOutputFile(getOutputMediaFile().toString()); 
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 

    mMediaRecorder.setVideoEncodingBitRate(2500000); 
    mMediaRecorder.setVideoSize(640, 480); 

这些参数在所有我曾试图支持这些设备(4.0和以上),但如果我的代码的最后一行更改为:

mMediaRecorder.setVideoSize(960, 540); 

然后发生错误:

java.lang.RuntimeException: start failed. 
android.media.MediaRecorder.start(Native Method) 

任何人都可以帮助,因为我想以960x540分辨率录制视频。

+0

手动大小不适用于所有设备 – Amsheer 2014-11-21 10:31:36

+0

感谢您的帮助:) – 2014-11-21 10:33:42

+0

您的问题是不是完全固定 – Amsheer 2014-11-21 10:34:08

回答

1

使用此代码,这将帮助:

try { 
      recording = true; 
      mrec = new MediaRecorder(); 
      mCamera.unlock(); 
      mrec.setCamera(mCamera); 

      //Set audio source 
      mrec.setAudioSource(MediaRecorder.AudioSource.MIC); 
      //set video source 
      mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

      //set output format 
      mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

      int width = 0;//set whatever 
      int height = 0;//set whatever 
      try { 
       //get the available sizes of the video 
       List<Size> tmpList = getSupportedVideoSizes(); 

       final List<Size> sizeList = new Vector<Size>(); 

       // compare the apsect ratio of the candidate sizes against the 
       // real ratio 
       Double aspectRatio = (Double.valueOf(getWindowManager() 
         .getDefaultDisplay().getHeight())/getWindowManager() 
         .getDefaultDisplay().getWidth()); 
       for (int i = tmpList.size() - 1; i > 0; i--) { 
        Double tmpRatio = Double.valueOf(tmpList.get(i).height) 
          /tmpList.get(i).width; 
        if (EnableLog.LOG_TAG) { 
         Log.e("Width & height", tmpList.get(i).width + " x " 
           + tmpList.get(i).height); 
        } 
        if (Math.abs(aspectRatio - tmpRatio) < .15) { 
         width = tmpList.get(i).width; 
         height = tmpList.get(i).height; 
         sizeList.add(tmpList.get(i)); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      // set the size of video. 
      // If the size is not applicable then throw the media recorder stop 
      // -19 error 
      mrec.setVideoSize(width, height); 

      // Set the video encoding bit rate this changes for the high, low. 
      // medium quality devices 
      mrec.setVideoEncodingBitRate(1700000); 

      //Set the video frame rate 
      mrec.setVideoFrameRate(30); 

      //set audio encoder format 
      mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 

      //set video encoder format 
      mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 

      //Show the display preview 
      mrec.setPreviewDisplay(surfaceHolder.getSurface()); 

      //output file path 
      mrec.setOutputFile(output_path); 

      mrec.prepare(); 

      mrec.start(); 

     } catch (IllegalStateException e) { 

      e.printStackTrace(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

这也太:

public List<Size> getSupportedVideoSizes() { 
     if (params.getSupportedVideoSizes() != null) { 
      return params.getSupportedVideoSizes(); 
     } else { 
      // Video sizes may be null, which indicates that all the supported 
      // preview sizes are supported for video recording. 
      return params.getSupportedPreviewSizes(); 
     } 
    } 

我希望这将解决您的问题。

注意

更新4.4.2 getPreviewsizes()后的三星Galaxy Tab不起作用3(7" )(对我来说)。因此,我希望预览大小中的所有设备不工作,因此检查视频尺寸第一,如果回报率的大小,然后使用它,或者如果它返回null,则使用getPreviewsizes这是在我的代码。

,我想你有像媒体录像机开始另一条错误消息失败的错误-19?是什么呢?

相关问题