【发布时间】:2014-04-30 10:42:39
【问题描述】:
我正在尝试在单击视图对象时播放 SoundPool。
我已经成功实现了,但是发现点击的声音会有延迟。
我一直在研究 StackOverFlow 和互联网上的解决方案,最好的解决方案似乎是将 SoundPool 放在单独的线程上。
我对整个 Thread 事情很陌生,所以我现在真的不知道该怎么做。
我已按照THIS 指南创建 SoundPool 线程,但是,我不知道如何访问它,甚至不知道如何分配我希望播放的剪辑。
谁能给我一个示例,说明在单击视图中的对象时我应该如何调用 SoundPool 线程来播放我的剪辑。
请在下面找到我的 onClick 代码:
@Override
public boolean onTouch(View v, MotionEvent event) {
int actionPerformed = event.getAction();
switch(actionPerformed) {
case MotionEvent.ACTION_DOWN: {
if (Math.pow((event.getX() - a_Ball.getX()),2)
+ Math.pow((event.getY() - a_Ball.getY()), 2)
<= Math.pow(a_Ball.getDiameter(),2)) {
//pCheck = !pCheck;
if (pauseBall == false) {
SoundPool soundPool = new SoundPool(1,AudioManager.STREAM_MUSIC,1);
clickSound = soundPool.load(MainActivity.this, R.raw.click, 1);
/*soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
soundPool.play(clickSound,1.0f, 1.0f, 0, 0, 1.0f);
}
});*/
//Increase Ball Speed
sIncrease = true;
}
} else {
if (pauseBall == false) {
//set minus health when not touch on ball
health--;
TextView txtHealth = (TextView)findViewById(R.id.health);
String tempHealth = txtHealth.getText().toString();
tempHealth = getResources().getString(R.string.health) + String.valueOf(health);
txtHealth.setText(tempHealth);
}
}
break;
}
default:
return false;
}
return true;
} //onTouch end
提前致谢。
【问题讨论】:
标签: android multithreading onclick soundpool