gpt4 book ai didi

java - Android 媒体记录器停止异常

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

我正在从 android 媒体记录器录制视频和音频...
我正在检查剪辑长度,如果少于 2 秒,请将其删除...
但问题是,如果我从文件路径检查文件的持续时间,MediaRecorder 每次抛出异常后都会抛出异常,即使我录制了 10 秒的音频?
但是当我评论代码以检查创建的视频的持续时间时,它工作正常......
以下是我的代码

if (prMediaRecorder != null) {
try {
prMediaRecorder.stop();
timer.cancel();
PathNameArray.add(prRecordedFile.getPath());
Log.e("No Exception", "File Added and Saved");

////////////// Check Length and Delete File
if (prRecordedFile != null) {
if (MediaPlayer.create(getApplicationContext(), Uri.fromFile(new File(
prRecordedFile.getPath()))).getDuration() <= 2000) {

File file = new File(prRecordedFile.getPath());
boolean deleted = file.delete();
Toast.makeText(getApplicationContext(),
"Video Clip Length Too Short, Clip Not Added",
Toast.LENGTH_SHORT).show();

PathNameArray.remove(PathNameArray.size() - 1);
}
}

} catch (RuntimeException e) {
Toast.makeText(getApplicationContext(),
"Corrupt Clip, Clip Not Added",
Toast.LENGTH_SHORT).show();
File file = new File(prRecordedFile.getPath());
boolean deleted = file.delete();
timer.cancel();
Log.e("Exception Caught", "File Not Added");

} finally {
try {
prCamera.reconnect();
} catch (IOException e) {

e.printStackTrace();
}
}
}

prMediaRecorder = new MediaRecorder();
MarkerName = null;

请帮帮我,我的代码有问题还是什么?

最佳答案

我解决了这个问题,
问题是我从 MediaPlayer
获取持续时间的方式相反,我使用了这段代码,它被修复了……

//////////////// Check Length and Delete File
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(prRecordedFile.getPath());
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong(time);

关于java - Android 媒体记录器停止异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522656/

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