Gil*_*ead 11 audio android duration media-player soundpool
我正在使用SoundPool 在我的应用程序中播放音频剪辑.一切都很好,但我需要知道剪辑播放何时结束.
目前,我通过使用MediaPlayer实例获取每个剪辑的持续时间来在我的应用中跟踪它.这工作正常,但加载每个文件两次看起来很浪费,只是为了得到持续时间.我可以自己知道文件的长度(可从AssetFileDescriptor获得)粗略计算持续时间,但我仍然需要知道采样率和通道数.
我看到了这个问题的两种可能解决方案:
有什么建议?
谢谢,马克斯
我目前正在使用的代码(工作正常,但为此目的而言相当沉重):
String[] fileNames = ...
MediaPlayer mp = new MediaPlayer();
for (String fileName : fileNames) {
AssetFileDescriptor d = context.getAssets().openFd(fileName);
mp.reset();
mp.setDataSource(d.getFileDescriptor(), d.getStartOffset(), d.getLength());
mp.prepare();
int duration = mp.getDuration();
// ...
}
Run Code Online (Sandbox Code Playgroud)
在旁注中,这个问题已被提出但没有得到答案.
Meh*_*her 12
你试过这个:
String mediaPath = Uri.parse("android.resource://<your-package-name>/raw/filename").getPath();
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mediaPath);
String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
mmr.release();
Run Code Online (Sandbox Code Playgroud)
但我不确定这比现有代码更轻.
| 归档时间: |
|
| 查看次数: |
14053 次 |
| 最近记录: |