Reputation: 3624
I am recording both video and audio from the android media recorder...
I am checking the clip length, if less then 2 sec, delete it...
But the problem is if i check the duration of file from file path, MediaRecorder throws exception everytime after once exception is thrown, even when i record audio of 10 seconds?
But when i comment the code to check the duration of video created, it worsk fine...
Following is my code
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;
Please Help me out, something wrong with my code or what ?
Upvotes: 3
Views: 818
Reputation: 3624
I fixed the issue,
The problem was the way i was getting the duration, from MediaPlayer
Instead i used this code and it was fixed...
//////////////// 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);
Upvotes: 1