我目前正在网上学习游戏开发。我想添加声音fx,让游戏更有趣。程序布局包含MainActivity()
和3个主题:GameView()
,GameThread()
和TheGame()
。 TheGame()
是所有更新的循环。我创建了一个用于播放声音的新类:
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundFX {
public static final int S1 = R.raw.ball;
public static final int S2 = R.raw.balloon;
private static SoundPool mSoundPool;
private static HashMap mSoundMap;
public static void initSounds(Context context){
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
mSoundMap = new HashMap(2); // 2 sounds in sound map
//Put sounds into SoundMap
mSoundMap.put( S1, mSoundPool.load(context, R.raw.ball, 1) );
mSoundMap.put( S2, mSoundPool.load(context, R.raw.balloon, 2) );
}//Initiate Sounds
public static void playSound(Context context, int soundID) {
//No sounds yet loaded..... then Load
if(mSoundPool == null || mSoundMap == null){
initSounds(context);
}
float volume = 1 ; // whatever in the range = 0.0 to 1.0
// play sound with same right and left volume, with a priority of 1,
// zero repeats (i.e play once), and a playback rate of 1f
mSoundPool.play(soundID, volume, volume, 1, 0, 1f);
}//Play
}//SoundFX
但是当我在TheGame
循环中检测到碰撞时,我想打电话给班级并播放声音。
我尝试过使用:
SoundFX.playSound(S1);
但Eclipse正在说The Method playSound(Context, int) is not applicable for the argument (int)
有人可以帮忙吗?
答案 0 :(得分:1)
您没有传递上下文。
SoundFX.playSound(this, S1);