这是我的主要活动,我相信这就是你所需要的,看看是否有人可以帮助我......
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++?