gpt4 book ai didi

java - 无法在 Android MediaRecorder 中设置手动视频大小

转载 作者:行者123 更新时间:2023-11-30 11:12:06 24 4
gpt4 key购买 nike

我在 Android 中使用 MediaRecorder 来录制视频,mu 当前参数是:

    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 分辨率录制视频。

最佳答案

使用这段代码会有帮助:

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() 在 Samsung Galaxy Tab 3(7") 中不起作用(对我而言)。所以我希望预览尺寸不适用于所有设备。因此,请先检查视频尺寸,如果返回尺寸,则使用它,或者如果它返回 null,则使用我的代码中的 getPreviewsizes。

我想您收到了另一条错误消息,例如媒体记录器启动失败错误 -19?是吗?

关于java - 无法在 Android MediaRecorder 中设置手动视频大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com