我制作了一款录音应用程序,我想在列表视图中显示录音的持续时间。 我是这样保存录音的:
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();
}
如何获取此文件的持续时间(以秒为单位)?
提前感谢。
---编辑 我搞定了,我在MediaPlayer.setOnPreparedListener()方法中调用了MediaPlayer.getDuration(),所以它返回了0。
MediaMetadataCompat
,但是MediaMetadataRetriever.METADATA_KEY_DURATION
已经过时了。最好像这样获取持续时间:`String durationStr = mmr.ExtractMetadata(MetadataKey.Duration);`
- dstrube