How to know the length of an audio recording in An

2019-06-04 02:29发布

问题:

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!

回答1:

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


回答2:

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