Jac*_*ień 5 android soundmanager2
我有一个小应用程序,基本上设置一个计时器,一个接一个地播放2个声音的集合.
我尝试了2个计时器,因为我希望每次都能在两个声音的同时开始.我给应用程序500ms用于在开始之前设置两个计时器
Calendar cal = Calendar.getInstance();
Date start = new Date(cal.getTime().getTime() + 500);
timerTask1 = new TimerTask() { //1st timer
@Override
public void run() {
soundManager.playSound(1);
}
};
timer1 = new Timer();
timer1.schedule(timerTask1, start, 550);
timerTask2 = new TimerTask() { //2nd timer
@Override
public void run() {
soundManager.playSound(2);
}
};
timer2 = new Timer();
timer2.schedule(timerTask2, start, 550);
}
Run Code Online (Sandbox Code Playgroud)
soundManager是一个基于本教程的SoundManager对象.因为我同时只播放2个声音,所以我所做的唯一改变就是将可用流的数量从20减少到2.
现在问题.它不是在我的Motorola RAZR或仿真器上以相同的速率播放.应用程序有时会减速,制动时间比预期长.我不能让这种情况发生.这可能有什么问题?
我在OGG格式中使用非常短的声音
编辑:我做了一些研究.使用了2个aproaches.我正在测量声音之间的毫秒距离.刷新率为500毫秒.
最后,没有任何一个方法能够顺利播放.总是有滞后的
我知道它可能有点太多了,但您可以尝试不同的SoundPool实现:
public class Sound {
SoundPool soundPool;
int soundID;
public Sound(SoundPool soundPool, int soundID) {
this.soundPool = soundPool;
this.soundID = soundID;
}
public void play(float volume) {
soundPool.play(soundID, volume, volume, 0, 0, 1);
}
public void dispose() {
soundPool.unload(soundID);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,你可以这样做:
SoundPool soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
AssetFileDescriptor assetFileDescriptor = activity.getAssets().openFd(fileName);
int soundID = soundPool.load(assetFileDescriptor, 0);
Sound sound = new Sound(soundPool, soundID);
Run Code Online (Sandbox Code Playgroud)
然后玩:
sound.play(volume);
Run Code Online (Sandbox Code Playgroud)
PS如果问题仍然存在,即使使用此代码,发表评论我会回复你.
| 归档时间: |
|
| 查看次数: |
1146 次 |
| 最近记录: |