Edited: from class Activity:
private Sounds s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
s = new Sounds(this);
s.initSounds();
}
You can also send the context with the constructor to your own class.
Delete static variables and methods:
public class Sounds {
private boolean sound = true;
private int FLIP_SOUND = 1;
private Context context;
private SoundPool soundPool;
private HashMap soundPoolMap;
public Sounds(Context context){
this.context = context;
soundPoolMap = new HashMap();
soundPool = new SoundPool(0, AudioManager.STREAM_MUSIC, 0);
}
public void initSounds() {
soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
}
public void playFlip() {
soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1);
}
public void setSound(Boolean onOff) {
sound = onOff;
}
}
source
share