随着发行声音管理器播放随机文件 [英] Issue With Sound Manager Playing Random Files
问题描述
我试图用SoundManager类随机播放30秒的声音咬,同时开始由onTouch事件触发的动画序列(闪烁图形)。无论出于何种原因,叮的一声被约5秒钟播放剪辑后,我想不通为什么。有什么想法?
I am trying to use the SoundManager to randomly play a 30 second sound bite while simultaneously starting an animation sequence (flashing graphic) triggered by an onTouch event. For whatever reason, the sound bite is clipped after about 5 seconds of playback and I can't figure out why. Any thoughts?
同样经过测试,看来这不会玩,直到最初onTouch事件发生后一分钟左右。
Also after testing, it appears that the will not play until a minute or so after the initial onTouch event.
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0, R.raw.sound0);
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
};
public boolean onTouchEvent(MotionEvent evt){
Random r = new Random();
int x = r.nextInt(3);
switch (evt.getAction())
{
case MotionEvent.ACTION_DOWN:
mSoundManager.playSound(x);
startAnimating();
return super.onTouchEvent(evt);
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return true;
}
private void startAnimating() {
ImageView wiub_screen01 = (ImageView) findViewById(R.id.wiub_screen01);
Animation fadein01 = AnimationUtils.loadAnimation(this, R.anim.fade_in01);
wiub_screen01.startAnimation(fadein01);
ImageView wiub_screen00 = (ImageView) findViewById(R.id.wiub_screen00);
Animation fadein00 = AnimationUtils.loadAnimation(this, R.anim.fade_in00);
wiub_screen00.startAnimation(fadein00);
}
}
推荐答案
有一个很好的机会,音频尚未加载。如果你真的想打一些短片,绝对看的Soundpool代替。 (从谷歌的Soundpool实例改编)
There's a good chance that the audio hasn't been loaded. If you really want to play a number of short clips, definitely look at SoundPool instead. (Adapted from Google SoundPool Example)
试试这个:
public class Soundboard extends Activity {
private SoundPool mSoundPool;
boolean loaded = false;
private int[] soundIDs;
private int[] resIDs;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
resIDs = new int[]{R.raw.sound0,R.raw.sound1,R.raw.sound2.R.raw.sound3 };
for(int i=0; i<resIDs.length; i++){
soundIDs[i] = mSoundPool.load(this, resIDs[i], 1);
}
};
public boolean onTouchEvent(MotionEvent evt){
Random r = new Random();
int x = r.nextInt(resIDs.length);
switch (evt.getAction())
{
case MotionEvent.ACTION_DOWN:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
mSoundPool.play(soundIDs[x], volume, volume, 1, 0, 1f);
Log.i("Test", "Played sound");
}
startAnimating();
return super.onTouchEvent(evt);
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return true;
}
private void startAnimating() {
ImageView wiub_screen01 = (ImageView) findViewById(R.id.wiub_screen01);
Animation fadein01 = AnimationUtils.loadAnimation(this, R.anim.fade_in01);
wiub_screen01.startAnimation(fadein01);
ImageView wiub_screen00 = (ImageView) findViewById(R.id.wiub_screen00);
Animation fadein00 = AnimationUtils.loadAnimation(this, R.anim.fade_in00);
wiub_screen00.startAnimation(fadein00);
}
}
这篇关于随着发行声音管理器播放随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!