Sounds effects with Minim

Recommended way to play a sound sample:
1) Import minim

import ddf.minim.*;

2) Initialize minim

minim = new Minim(this);

3) declare AudioPlayer object as global

AudioPlayer c2;

4) load an audio file in setup

c2 = minim.loadFile("c2.wav");

5) rewind and play the audioplayer

c2.rewind();
c2.play();
//or loop(n) for a loop
music.loop(3);

full example:

//sound with minim
 
//import minim, the sound library
import ddf.minim.*;
 
//Minim is the library it requires a main object
Minim minim;
 
//AudioPlayer is a object that contains your soundfile
AudioPlayer c2;
AudioPlayer e2;
AudioPlayer g1;
AudioPlayer music;
 
void setup() {
  //sketch size
  size(800, 600);
 
  //initialize the library 
  minim = new Minim(this);
 
  //load files
  c2 = minim.loadFile("c2.wav");
  e2 = minim.loadFile("e2.wav");
  g1 = minim.loadFile("g1.wav");
  music = minim.loadFile("percussion.mp3");
 
  //play the loop 3 times
  music.loop(3);
 
}
 
void draw() {
}
 
 
 
//this function is called when a key is pressed
void keyPressed() {
 
  if(key == 'a')
  {
    //rewind before playing
    g1.rewind();
    g1.play();
  }
 
  if(key == 's')
  {
    c2.rewind();
    c2.play();
  }
 
  if(key == 'd')
  {
    e2.rewind();
    e2.play();
  }
 
 
}
 
//it's good practice to close minim at the end
//stop is called when you shut down the program
void stop()
{
  minim.stop();
}

COLLISIONS AND SOUNDS

Here’s an example putting minima and fisica together.
example12b – sound and collisions

Posted in

Post a comment