charles young
charles young

Reputation: 2289

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!

Upvotes: 1

Views: 2485

Answers (2)

R Earle Harris
R Earle Harris

Reputation: 983

I do this by using my input stream: is.available() on InputStream is gives me the length.

Upvotes: 0

MByD
MByD

Reputation: 137312

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();

Upvotes: 3

Related Questions