Manual: AudioSnippet

[ javadoc ]

If all you want to do is play a short audio file and you don’t need access to the samples and don’t need to apply any real-time effects, the best choice is to use an AudioSnippet. You can obtain an AudioSnippet from Minim by calling the loadSnippet method and passing the name of the file you want to load (in which case the file must be in the sketches data folder), an absolute path to the file, or a URL for the file. The file will then be completely loaded into memory and played back from there. This is important to note because if you are loading 50MB WAVs into AudioSnippets you will run out of memory pretty quickly. This will be equally true if you are loading 5 minute mp3s because the audio will be uncompressed as it is loaded into memory and will take up just as much memory as a 5 minute wav file (about 50MB). If you want to play large files use an AudioPlayer.

Code Sample

import ddf.minim.*;
 
AudioSnippet song;
 
void setup()
{
  size(200, 200);
  // always start Minim first
  Minim.start(this);
  // load a file into an AudioSnippet
  // it must be in this sketches data folder
  song = Minim.loadSnippet("mysong.wav");
}
 
void draw()
{
  background(0);
}
 
void keyPressed()
{
  if ( key == 'p' )
  {
    song.play();
  }
}
 
void stop()
{
  // always close Minim audio classes
  song.close();
  super.stop();
}

An AudioSnippet is Playable, which is why we are able to call play() in the above example. Please refer to the Playable section for a description of how to use that interface.

Also note that the snippet is closed before exiting the program. As with all audio I/O classes in Minim, you should call the close method when you are finished using the object. This allows the thread managing playback to finish properly and frees resources used by that thread. You can choose to close a snippet somewhere other than in the stop method, like if you wanted to assign a new snippet to song you’d need to call close before assigning a new snippet. If you didn’t do this, the thread for the original snippet would continue executing and you’d have no way to stop it.

Other Controls

While an AudioSnippet doesn’t give you access to the samples as they are played, it does derive from Controller and as such has all of its methods available, which are described in the section about Controller.