最近在使用MediaRecorder录制视频的时候遇到crash

本文探讨了使用MediaRecorder录制视频时遇到的质量问题,并提供了一种通过设置CamcorderProfile来提高视频质量的方法。同时,文章详细解释了如何避免在设置视频格式时出现的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置视频源
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
        //设置输出格式
        recorder.setOutputFormat(OUTPUT_FORMAT);
        recorder.setAudioEncoder(AUDIO_ENCODER);
        //设置视频编码
        recorder.setVideoEncoder(VIDEO_ENCODER);
        // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
        recorder.setVideoSize(width, height);
        // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
        recorder.setVideoFrameRate(frameRate);
        recorder.setOrientationHint(90);
        recorder.setPreviewDisplay(surfaceHolder.getSurface());

        recorder.setOutputFile("/sdcard/v.mp4");
        //disable this limit
        recorder.setMaxDuration(0);
        recorder.setMaxFileSize(0);
        recorder.setOnErrorListener(this);
        recorder.setOnInfoListener(this);


在没有setProfile的时候能正常工作,但是视频质量不行,

recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
于是报出

setOutputFormat called in an invalid state
后来在stackoverflow找到答案

http://stackoverflow.com/questions/21632769/setoutputformat-called-in-an-invalid-state-4-where-and-why

public void setProfile(CamcorderProfile profile) {
  setOutputFormat(profile.fileFormat);
  setVideoFrameRate(profile.videoFrameRate);
  setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
  setVideoEncodingBitRate(profile.videoBitRate);
  setVideoEncoder(profile.videoCodec);
  if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW &&
      profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) {
   // Nothing needs to be done. Call to setCaptureRate() enables
   // time lapse video recording.
  } else {
    setAudioEncodingBitRate(profile.audioBitRate);
    setAudioChannels(profile.audioChannels);
    setAudioSamplingRate(profile.audioSampleRate);
    setAudioEncoder(profile.audioCodec);
  }
}

原来在setProfile里面把这些事情都做了,不需要再重复做






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值