gpt4 book ai didi

java - 获取音频文件的持续时间 - Android

转载 作者:行者123 更新时间:2023-12-01 18:22:56 26 4
gpt4 key购买 nike

我正在尝试通过 ListView 单击音频文件的持续时间。在此 ListView 中,我正确显示了它们的名称,并且我能够播放它们,但我想知道其持续时间。

我在 Stackoverflow 中寻找了不同的问题,这些问题实际上似乎回答了这个问题,但我并不能真正做到这一点,所以我将在这里留下我的尝试。我试图遵循这个answer但它不起作用,所以我假设我正在获取或理解 Uri

文件存储在/raw/

JAVA - 第一次尝试

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// play desired audio
Field[] fields = R.raw.class.getFields();
if(soundMap.selectedEncoder != null) {
//Log.d(TAG, String.valueOf(soundMap.getX()) + " " + String.valueOf(soundMap.getY()));
soundMap.selectedEncoder.play(position, audioFiles[position]);

// Get duration of the audio file
Uri uri = Uri.parse("/raw/"+fields[position].getName());
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(getApplicationContext(), uri);
String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
//int audioFileDuration = Integer.parseInt(durationStr);

}
}
});

音频存储在raw文件夹中。我还尝试将 .mp3 添加到 uri

错误

E/AndroidRuntime: FATAL EXCEPTION: main Process: myapp, PID: 26303 java.lang.IllegalArgumentException

JAVA - 第二次尝试

File audioFile = new File("/raw/"+fields[position].getName()+ ".mp3");
mmr.setDataSource(context, Uri.fromFile(audioFile));
String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time );

编辑 - 第三次尝试最后我使用了MediaPlayer

AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.andthenturnleft);
if (afd == null) return;
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
e.printStackTrace();
}

int audioFileDuration = mediaPlayer.getDuration();
/*try {

我得到了文件的持续时间。尽管如此,首先我收到以下错误:

E/MediaPlayerNative: Attempt to call getDuration in wrong state: mPlayer=0xf6f4e7a0, mCurrentState=2

我认为生成错误是因为在第一次尝试获取持续时间期间,MediaPlayer 尚未准备好。我想停止代码直到准备好。尽管如此,最终我得到的 audioFileDuration 的值为 24 这是不可能的,因为它应该返回以毫秒为单位的值,而文件的长度为 2 秒。

第四次尝试

String path = context.getFilesDir().getAbsolutePath();
try {
mediaPlayer.setDataSource(path + fields[position].getName() + ".mp3");
} catch (IOException e) {
e.printStackTrace();
}

int audioFileDuration = mediaPlayer.getDuration();

问题是这种情况下我得到的值为 0,因为它找不到该文件

open failed: ENOENT (No such file or directory)

如何正确获取文件的路径

已解决我已经发布了我自己的解决方案。我只需要找到一种新的方法来设置文件的路径,我终于成功了。

最佳答案

    implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'

FFmpegMediaMetadataRetriever metadataRetriever = new FFmpegMediaMetadataRetriever();
metadataRetriever = new FFmpegMediaMetadataRetriever();
metadataRetriever.setDataSource(url);
LogMessage.v("1::Duration" + retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));

关于java - 获取音频文件的持续时间 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com