这是我的主要活动,我相信所有你需要看看是否有人可以帮助我....
package com.art.drumdrum;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
SoundPool soundPool = null;
int kickId = 0;
int snareId = 0;
int crashhId = 0;
int crashlId = 0;
int muteId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button kick = (Button) findViewById(R.id.kick);
Button snare = (Button) findViewById(R.id.snare);
Button crashh = (Button) findViewById(R.id.crashh);
Button crashl = (Button) findViewById(R.id.crashl);
kick.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
soundPool.play(kickId, 1, 1, 0, 0, 1);
return false;
}
});
snare.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
soundPool.play(snareId, 1, 1, 0, 0, 1);
return false;
}
});
crashh.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
soundPool.play(crashhId, 1, 1, 0, 0, 1);
return false;
}
});
crashl.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
soundPool.play(crashlId, 1, 1, 0, 0, 1);
return false;
}
});
}
protected void onResume() {
super.onResume();
if (soundPool == null) {
soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
kickId = soundPool.load(this, R.raw.kick, 1);
snareId = soundPool.load(this, R.raw.snare, 1);
crashhId = soundPool.load(this, R.raw.crashh, 1);
crashlId = soundPool.load(this, R.raw.crashl, 1);
}
}
protected void onPause() {
super.onPause();
if (soundPool != null) {
soundPool.release();
soundPool = null;
}
}
}
我想知道在我尝试做什么的情况下,使用JetPlayer是否会比soundPool更好。我有它,所以我可以同时打多个按钮和声音播放,一切看起来都很好,但是当你按下按钮直到它播放声音时会有一个小的延迟,使声音变得不自然....我我一直在查找信息并找不到任何帮助,是否有简单的答案或是需要更多代码甚至可能是某些c ++的东西?
答案 0 :(得分:4)
据我所知,JetPlayer不适用于此,因为它旨在实现MIDI音频同步。 在Jet音频引擎中,除非您停止所有以前播放的声音(JetPlayer#clearQueue()),否则在按下按钮时无法播放声音。
对于Android音频延迟问题,没有简单的答案。 使用C ++(OpenSL ES)可能会获得更好的延迟,但我对此并不了解。 请参阅相关答案:https://stackoverflow.com/a/12309643