Manual: AudioPlayer

[ javadoc | examples ]

If you want to play long audio files you should use an AudioPlayer. This will cause the file to be streamed from its location. You can obtain an AudioPlayer from Minim by calling one of the loadFile methods and passing the name of the file you want to load and optionally the size of the buffer used for monitoring. The name of the file can be just the filename, like “mysong.wav”, in which case Minim will look in all of the places Processing looks (the data folder, the sketch folder, etc), or it can be the full path to the file, or it can be a URL. The disadvantage of using an AudioPlayer is a slight increase in latency because the file is streaming, rather than being totally read into memory and played from there (like with an AudioSnippet or AudioSample). The latency could especially be an issue if you are playing a compressed format like mp3 because it will be decompressed on-the-fly.

Code Sample (online example)

import ddf.minim.*;
 
AudioPlayer groove;
WaveformRenderer waveform;
 
void setup()
{
  size(512, 200);
  // always start Minim first
  Minim.start(this);
  groove = Minim.loadFile("groove.mp3", 512);
 
  waveform = new WaveformRenderer();
  // see the example Recordable >> addListener for more about this
  groove.addListener(waveform);
}
 
void draw()
{
  background(0);
  waveform.draw();
}
 
void keyPressed()
{
  if ( key == 'p' ) groove.play();
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  groove.close();
  // always stop Minim before exiting
  Minim.stop();
 
  super.stop();
}

An AudioPlayer 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. An AudioPlayer is Recordable, which is why we are able to call the addListener method in the above example. Please refer to the Recordable section for a description of how to use that interface. Not shown here is the fact that an AudioPlayer is also Effectable, meaning we could have added AudioEffects to the player, like a low pass filter. Please refer to the Effectable section for a description of how to use that interface.

Also note that the player 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 player somewhere other than in the stop method, like if you wanted to assign a new player to groove you’d need to call close() before assigning a new player. If you didn’t do this, the thread for the original player would continue executing and you’d have no way to stop it.

Other Controls

AudioPlayer derives from AudioSource, this is where it gets its Recordable and Effectable interfaces from. AudioSource derives from Controller and as such AudioPlayer has all of its methods available, which are described in the section about Controller.