While minim is a great library, it often does a lot more than I need and adds an additional dependence and several libraries. So I usually just add a java sound trigger. It doesn't support MP3, but works for triggering basic sound effects. We should consider adding this to the core code for simple sounds.
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.AudioInputStream;
- AudioClip button1, button2;
- void setup() {
- button1 = new AudioClip("sound1.wav");
- button2 = new AudioClip("sound2.wav");
- }
- void draw() {
- //something that would trigger the sound...
- button1.trigger();
- }
- class AudioClip {
- Clip clickClip;
- public AudioClip(String aspath) {
- try {
- File mediafile = new File(dataPath(aspath));
- AudioInputStream ais = AudioSystem.getAudioInputStream(mediafile);
- this.clickClip = AudioSystem.getClip();
- this.clickClip.open(ais);
- }
- catch (Exception e) {
- }
- }
- public void trigger() {
- this.clickClip.setFramePosition(0);
- this.clickClip.start();
- }
- public void close() {
- this.clickClip.stop();
- this.clickClip = null;
- }
- }