MediaRecorder准备失败,使用setVideoSize 1080p

时间:2015-10-04 22:23:56

标签: android mediarecorder

我的项目中有一个MediaRecorder对象,其初始化如下:

private void initRecorder() {
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
    mMediaRecorder.setVideoFrameRate(60);
    mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    String filepath_external = Environment.getExternalStorageDirectory().getAbsolutePath()  + "/test/";
    File f = new File(filepath_external);
    if(!f.exists()){
        f.mkdirs();
    }
    mMediaRecorder.setOutputFile(filepath_external + System.currentTimeMillis() + ".mp4");
}

如果我设置

private static int DISPLAY_WIDTH = 1024;
private static int DISPLAY_HEIGHT = 768;

并调用initRecorder()后跟mMediaRecorder.prepare(),一切正常。

但是,我想用智能手机屏幕的大小进行录制。所以我使用以下代码来设置宽度和高度:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
DISPLAY_WIDTH = size.x;
DISPLAY_HEIGHT = size.y;

现在宽度= 1920,高度= 1080(基本上是1080p)。结果是我的活动甚至没有开始,产生了以下错误:

  

E / MediaRecorder:准备失败:-2147483648

     

W / System.err:java.io.IOException:prepare failed。

     

W / System.err:在android.media.MediaRecorder._prepare(原生方法)

     

W / System.err:在android.media.MediaRecorder.prepare(MediaRecorder.java:873)

     

W / System.err:at com.lfdversluis.testrecord.RecordActivity.prepareRecorder(RecordActivity.java:161)

为什么它不起作用,我怎样才能使它工作?

如果我在getSupportedVideoSizes()打印所有支持的视频尺寸,我会:

Optional size: 1920 1080 Optional size: 1440 1080 Optional size: 1280 720 Optional size: 800 450 Optional size: 720 480 Optional size: 640 480 Optional size: 320 240 Optional size: 176 144

所以应该支持1920年到1080年......

使用CamcorderProfile如下,我也得到1920×1080。同样,准备功能失败:

CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
int cameraWidth = camcorderProfile != null ? camcorderProfile.videoFrameWidth : -1;
int cameraHeight = camcorderProfile != null ? camcorderProfile.videoFrameHeight : -1;
DISPLAY_WIDTH = cameraWidth;
DISPLAY_HEIGHT = cameraHeight;
Log.e("Campcorder profile", String.format("%s %s", DISPLAY_WIDTH, DISPLAY_HEIGHT));

1 个答案:

答案 0 :(得分:3)

在CommonsWare的帮助下,我发现了这个问题。

确保同时检查相机参数中的getSupportedPreviewFrameRates。硬编码60或30 fps(例如在Telecine情况下)可能无法始终在每台设备上运行。如果您使用CamcorderProfile,也请将帧速率设置为camcorderProfile.videoFrameRate;