滞后边打2的声音在计时器的同时 [英] Lags while playing 2 sounds on the same time in timer
问题描述
我有一个小的应用程序,基本上建立一个定时和戏剧套2的声音此起彼伏
I have a small app that basically sets up a timer and plays sets of 2 sounds one after another.
我试过2计时器,因为我想这两个听起来在同一时间每次都精确地启动。我给应用500ms的设置两个定时器才开始
I've tried 2 timers, because I wanted both sounds to start exactly at the same time each time. I gave the app 500ms for setting both timers before they start
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);
}
SoundManager类是一个 SoundManager类这是基于对象的this教程。 Thew只改变我做减少了从20到2 avalible流的数量,因为我在同一时间只能播放2声音。
soundManager is a SoundManager object which is based on this tutorial. Thew only change I've made was decreasing number of avalible streams from 20 to 2 since I play only 2 sounds at the same time.
现在的问题。 它不是在相等的速度播放既不是我的摩托罗拉RAZR或仿真器。该应用程序有时会减慢,使得比预期更长的刹车。我不能让这种情况发生。可能是什么错在这里?
Now the problem. It's not playing at the equal rate neither on my Motorola RAZR or the emulator. The app slows sometimes, making a longer brake than desired. I can't let that happen. What could be wrong here?
我用很短的声音在 OGG 格式
编辑:
我做了一些研究。使用2 aproaches。我被测量毫秒之间的距离的声音被解雇了。刷新率为500毫秒。
I've made some research. Used 2 aproaches. I was measuring milisecond distances between sound was fired. Refresh rate was 500 ms.
- 1的形式给出了TimerTask的 - 这是一个很大的失败。它开始在300毫秒,然后不断壮大,并经过一段时间(2分钟)在497ms,如果它开始作为这将是蛮好的稳定。
- 第2的形式给出了,而在AsyncTask的循环 - 这是给我的输出从475 500毫秒,比TimerTask的更好,但仍然不准确
目前没有结束的形式给出了被打的顺利进行。有总是滞后
At the end none of aproach was playing smoothly. There were always lags
推荐答案
我知道这可能是太有点多,但你可以尝试不同的实现的Soundpool的:
I know it may be a little too much, but you could try a different implementation of 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);
}
}
要使用它,你可以做这样的事情:
To use it you can do something like this:
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);
然后播放:
sound.play(volume);
P.S。如果问题即使有这样的code仍然存在,发表评论,我会回来给你。
P.S. If the problem persists even with this code, post a comment and I'll get back to you.
这篇关于滞后边打2的声音在计时器的同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!