How to know the length of an audio recording in Android
I am using the following code to record audio:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(开发者_开发技巧"audio/*");
startActivityForResult(Intent.createChooser(intent, "Select audio source"), CardList.ACTIVITY_RECDAUDIO);
When the result comes back I do the following:
Uri u = intent.getData();
String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
I would like to know how long the recording is in seconds. Is it possible to query this somehow? If all else fails I can play the clip programatically and time it, but I would prefer a more direct method if possible. Thanks!
I don't know if this is fast enough for you. But in case you don't know - you don't have to actually play it. It is enough to create MediaPlayer
instance and set the path of the file and the call getDuration()
.
MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(path));
int duration = mp.getDuration();
I do this by using my input stream: is.available() on InputStream is gives me the length.
精彩评论