I have made a voice recorder app, and I want to show the duration of the recordings in a listview. I save the recordings like this:
MediaRecorder recorder = new MediaRecorder(); recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Audio recordings"); String[] files = folder.list(); int number = files.length + 1; String filename = "AudioSample" + number + ".mp3"; File output = new File(Environment.getExternalStorageDirectory() + File.separator + "Audio recordings" + File.separator + filename); FileOutputStream writer = new FileOutputStream(output); FileDescriptor fd = writer.getFD(); recorder.setOutputFile(fd); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); e.printStackTrace(); }
How can I get the duration in seconds of this file?
Thanks in advance
---EDIT I got it working, I called MediaPlayer.getduration() inside the MediaPlayer.setOnPreparedListener() method so it returned 0.
Using Audio Tag. The easiest way to obtain the duration of an audio file is through the embed Audio tag, used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the <source> element nested on the audio tag.
You can use the MediaMetadataRetriever to get the duration of a song. Use the METADATA_KEY_DURATION in combination with the extractMetadata() funciton.
MediaMetadataRetriever
is a lightweight and efficient way to do this. MediaPlayer
is too heavy and could arise performance issue in high performance environment like scrolling, paging, listing, etc.
Furthermore, Error (100,0)
could happen on MediaPlayer
since it's a heavy and sometimes restart needs to be done again and again.
Uri uri = Uri.parse(pathStr); MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(AppContext.getAppContext(),uri); String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); int millSecond = Integer.parseInt(durationStr);
The quickest way to do is via MediaMetadataRetriever
. However, there is a catch
if you use URI and context to set data source you might encounter bug https://code.google.com/p/android/issues/detail?id=35794
Solution is use absolute path of file to retrieve metadata of media file.
Below is the code snippet to do so
private static String getDuration(File file) { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.setDataSource(file.getAbsolutePath()); String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); return Utils.formateMilliSeccond(Long.parseLong(durationStr)); }
Now you can convert millisecond to human readable format using either of below formats
/** * Function to convert milliseconds time to * Timer Format * Hours:Minutes:Seconds */ public static String formateMilliSeccond(long milliseconds) { String finalTimerString = ""; String secondsString = ""; // Convert total duration into time int hours = (int) (milliseconds / (1000 * 60 * 60)); int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60); int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000); // Add hours if there if (hours > 0) { finalTimerString = hours + ":"; } // Prepending 0 to seconds if it is one digit if (seconds < 10) { secondsString = "0" + seconds; } else { secondsString = "" + seconds; } finalTimerString = finalTimerString + minutes + ":" + secondsString; // return String.format("%02d Min, %02d Sec", // TimeUnit.MILLISECONDS.toMinutes(milliseconds), // TimeUnit.MILLISECONDS.toSeconds(milliseconds) - // TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds))); // return timer string return finalTimerString; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With