你的代码看起来像这样吗?我强调了一些关键点
包be.domore.smokesignaltest;
导入android.app.Activity;导入android.os.Bundle;导入 android.view.MotionEvent;导入android.view.View;导入 android.view.View.OnTouchListener;导入android.widget.Button;
公共类 DummyActivity 扩展 Activity 实现 OnTouchListener { private Button myButton1; 私人按钮 myButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content view
setContentView(R.layout.activity_dummy);
// Map controls
mapControls();
}
private void mapControls() {
myButton1 = (Button) findViewById(R.id.myButton1);
myButton1.setOnTouchListener(this);
myButton2 = (Button) findViewById(R.id.myButton2);
myButton2.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
**if (event.getAction() == MotionEvent.ACTION_DOWN)**
switch (v.getId()) {
case R.id.myButton1:
// TODO: put your code here
**return true;**
case R.id.myButton2:
// TODO: put your code here
**return true;**
}
*return false;*
}
}
以下类是我创建的用于管理声音的助手。有趣的部分是函数 AssignSoundToView。不知道对你有没有帮助?
包be.domore.FingerTwister.Game;
导入 java.io.IOException;
导入 be.domore.FingerTwister.Settings;
导入android.app.Activity;导入 android.content.res.AssetFileDescriptor;导入 android.content.res.AssetManager;导入android.media.AudioManager;导入android.media.MediaPlayer;导入 android.media.SoundPool;导入android.util.Log;导入 android.view.MotionEvent;导入android.view.View;导入 android.view.View.OnTouchListener;
公共类 SoundManager2 { 私有静态 SoundManager2 当前 = 新 SoundManager2();
public static SoundManager2 getCurrent() {
return current;
}
private boolean initialized = false;
private MediaPlayer player;
private SoundPool pool;
private AssetManager assetManager;
private int[] soundId = new int[Sounds.values().length];
private int[] soundStream = new int[Sounds.values().length];
private SoundManager2() {
}
public void assignSoundToView(View view, Sounds sound) {
final int currentSound;
if (Settings.hasFx() == false) {
} else if (sound != Sounds.none) {
currentSound = sound.ordinal();
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
try {
if (event.getAction() == MotionEvent.ACTION_DOWN)
pool.play(soundId[currentSound], 1.0f, 1.0f, 0, 0,
1);
} finally {
}
return false;
}
});
}
}
public boolean initialize(Activity activity) {
if (initialized == false) {
player = new MediaPlayer();
pool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
assetManager = activity.getAssets();
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
for (int idx = 1; idx < Sounds.values().length; idx++)
soundId[idx] = loadSound(
String.format("%s.mp3", Sounds.values()[idx]),
assetManager);
initialized = true;
} else
Log.w("SoundManager", "SoundManager was already initialized");
return initialized;
}
public boolean isInitialized() {
return initialized;
}
private int loadSound(String filename, AssetManager assetManager) {
int rc;
AssetFileDescriptor afd;
try {
afd = assetManager.openFd(filename);
rc = pool.load(afd, 1);
} catch (IOException e) {
e.printStackTrace();
rc = 0;
}
return rc;
}
public boolean playMusic(String filename) {
boolean rc = false;
AssetFileDescriptor descriptor;
if (initialized == false)
Log.e("SoundManager", "SoundManager is not initialized");
else
try {
descriptor = assetManager.openFd(filename);
player.reset();
player.setDataSource(descriptor.getFileDescriptor(),
descriptor.getStartOffset(),
descriptor.getDeclaredLength());
player.setLooping(true);
// player.setVolume(1.0f, 1.0f);
player.prepare();
player.start();
rc = true;
} catch (Exception e) {
e.printStackTrace();
}
return rc;
}
public void playSound(Sounds sound) {
int index;
index = sound.ordinal();
soundStream[index] = pool.play(soundId[index], 1.0f, 1.0f, 0, 0, 1);
}
public void stopAllSound() {
for (int idx = 1; idx < Sounds.values().length; idx++)
stopSound(Sounds.values()[idx]);
}
public boolean stopMusic() {
boolean rc = false;
if (initialized == false)
Log.e("SoundManager", "SoundManager is not initialized");
else {
player.stop();
rc = true;
}
return rc;
}
public void stopSound(Sounds sound) {
int index;
index = sound.ordinal();
pool.stop(soundStream[index]);
}
}