gpt4 book ai didi

android - MediaRecorder 准备失败 : -2147483648 for nougat

转载 作者:太空狗 更新时间:2023-10-29 13:53:01 25 4
gpt4 key购买 nike

我正在编写一个应用程序来通过表面从相机录制视频。

以下代码用于准备MediaRecorder。

MediaRecorder mediaRecorder;
mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFilename);
mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}

像这样获取 outfile(videoFilename),

File defaultExternalFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File videoFolder = new File(defaultExternalFolder, "My-Vids");
if (!videoFolder.exists()) {
videoFolder.mkdir();
}

String timestemp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String prepend = "MyVid_" + timestemp + "_";
File videoFile = File.createTempFile(prepend, ".mp4", videoFolder);
videoFilename = videoFile.getAbsolutePath();
return videoFile;

videoSize 只是一个对象大小类。

当我尝试启动 MediaRecorder 时,我得到 java.io.IOException: prepare failed.

奇怪的是它仅在 Nougat OS 中崩溃,在其他设备中它工作正常。

最佳答案

我猜你是在模拟器上运行这段代码。实际上,模拟器不支持 MediaRecorder。参见:MediaRecorder

Note: Currently, MediaRecorder does not work on the emulator.

好吧,你可以通过评论 Mic config 来测试它,你会看到它的工作。

MediaRecorder mediaRecorder;
mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
//mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFilename);
mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}

我已经在 Android 7.1 (Samsung Galaxy S8) 上测试了您的代码及其工作情况。仅在模拟器上出现问题。

关于android - MediaRecorder 准备失败 : -2147483648 for nougat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43533163/

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