如何知道Android的音频录音的长度长度、音频、Android

2023-09-07 12:22:36 作者:鏇律ゞ

我用下面的code录制音频:

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

我想知道的记录以秒为单位的时间。是否有可能以某种方式查询呢?如果一切都失败,我可以编程播放的剪辑和时间,但我想preFER更直接的方法,如果可能的话。谢谢!

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!

推荐答案

我不知道这是不是对你不够快。但如果你不知道 - 你没有真正发挥它。这足以创建的MediaPlayer 实例,并设置文件的路径和呼叫 getDuration()

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