How to know the length of an audio recording in Android

馋奶兔 提交于 2019-12-23 02:22:06

问题


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.



来源:https://stackoverflow.com/questions/5849502/how-to-know-the-length-of-an-audio-recording-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!