我正在从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;
请帮帮我,我的代码有什么问题或什么?
答案 0 :(得分:1)
我解决了这个问题,
问题是我从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);