如何获得的Soundpool的持续时间 [英] How to get the duration of Soundpool
问题描述
我使用的Soundpool播放音频文件,我的目标是要播放一段音频文件,并完成后,再踢音频文件。
i am using soundpool to play audio files and my objective is to play a audio file and after finishing, play another audio file.
下面是我的code
String source_path = "/sdcard/varun/audio.mp3";
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
sound1 = mSoundPool.load(source_path, 1);
hand1.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
if (loaded) {
mSoundPool.play(sound1, 1, 1, 1, time - 1, 1);
System.out.println("playing=== a");
}
}
}, 21000);
我在这里硬编码值21000,但我需要得到的音频文件的持续时间,因为每个file.I时间的变化我对工作的android 2.2
Here i am hardcoding the values as 21000 but i need to get the duration of audio file because duration changes with each file.I am working on android 2.2
如何达致这请大家帮帮忙?
How to acheive this please help?
推荐答案
虽然看起来效率不高,你可以创建一个的MediaPlayer
与你想要的声音,然后使用 myMediaPlayer.getDuration()
办法给你毫秒剪辑。
While it seems inefficient, you could create a MediaPlayer
with your desired sound, and then use the myMediaPlayer.getDuration()
method to give you the milliseconds in the clip.
您可以调用 myMediaPlayer.release()
摆脱任何不需要的内存占用。虽然做更多的研究,我发现我不是唯一一个得出这样的结论。这个库:
You can then call myMediaPlayer.release()
to get rid of any undesired memory footprint. While doing more research I found that I was not the only one to come to this conclusion. This library:
使用来解决这个问题本确切方法。我认为这是应该做它一个合法的方式,因为这家伙看起来pretty合法的。
uses this exact method to solve the problem. I assume it is a legitimate way to do it, because this guy looks pretty legit.
这篇关于如何获得的Soundpool的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!