Minim core
ugens
analysis
Name AudioPlayer
Description An AudioPlayer provides a self-contained way of playing a sound file by streaming it from disk (or the internet). It provides methods for playing and looping the file, as well as methods for setting the position in the file and looping a section of the file. You can obtain an AudioPlayer by using the loadFile method of the Minim class.
Examples
/**
  * This sketch demonstrates how to play a file with Minim using an AudioPlayer. <br />
  * It's also a good example of how to draw the waveform of the audio. Full documentation 
  * for AudioPlayer can be found at http://code.compartmental.net/minim/audioplayer_class_audioplayer.html
  * <p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup()
{
  size(512, 200, P3D);
  
  // we pass this to Minim so that it can load files from the data directory
  minim = new Minim(this);
  
  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the 
  // sketch folder. you can also pass an absolute path, or a URL.
  player = minim.loadFile("groove.mp3");
}

void draw()
{
  background(0);
  stroke(255);
  
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }
  
  // draw a line to show where in the song playback is currently located
  float posx = map(player.position(), 0, player.length(), 0, width);
  stroke(0,200,0);
  line(posx, 0, posx, height);
  
  if ( player.isPlaying() )
  {
    text("Press any key to pause playback.", 10, 20 );
  }
  else
  {
    text("Press any key to start playback.", 10, 20 );
  }
}

void keyPressed()
{
  if ( player.isPlaying() )
  {
    player.pause();
  }
  // if the player is at the end of the file,
  // we have to rewind it before telling it to play again
  else if ( player.position() == player.length() )
  {
    player.rewind();
    player.play();
  }
  else
  {
    player.play();
  }
}
Constructors
Fields
left   The AudioBuffer containing the left channel samples. If this is a mono sound, it contains the single channel of audio.

mix   The AudioBuffer containing the mix of the left and right channels. If this is a mono sound, mix contains the same samples as left.

right   The AudioBuffer containing the right channel samples. If this is a mono sound, right contains the same samples as left.

Methods
addListener ( )   Add an AudioListener to this sound generating object.

bufferSize ( )   The internal buffer size of this sound object.

cue ( )   Sets the position to millis milliseconds from the beginning.

getBalance ( )   Returns the current balance.

getFormat ( )   Returns AudioFormat object that describes the audio properties of this sound generating object.

getGain ( )   Returns the current gain.

getMetaData ( )   Returns the meta data for the recording being played by this player.

getPan ( )   Returns the current pan.

getVolume ( )   Returns the current volume.

isLooping ( )   Returns true if the AudioPlayer is currently playing and has more than one loop left to play.

isMuted ( )   Returns true if the sound is muted.

isPlaying ( )   Indicates if the AudioPlayer is currently playing.

length ( )   Returns the length of the sound in milliseconds.

loop ( )   Set the AudioPlayer to loop some number of times.

loopCount ( )   Returns the number of loops left to do.

mute ( )   Mutes the sound.

pause ( )   Pauses playback.

play ( )   Starts playback from the current position.

position ( )   Returns the current position of the "playhead" in milliseconds (ie how much of the sound has already been played).

removeListener ( )   Removes an AudioListener that was previously added to this sound object.

rewind ( )   Rewinds to the beginning. This does not stop playback.

sampleRate ( )   Returns the sample rate of this sound object.

setBalance ( )   Sets the balance.

setGain ( )   Sets the gain.

setLoopPoints ( )   Sets the loop points used when looping.

setPan ( )   Sets the pan.

setVolume ( )   Sets the volume.

shiftBalance ( )   Transitions the balance from one value to another.

shiftGain ( )   Transitions the gain from one value to another.

shiftPan ( )   Transitions the pan from one value to another.

shiftVolume ( )   Transitions the volume from one value to another.

skip ( )   Skips millis milliseconds from the current position.

type ( )   The type is an int describing the number of channels this sound object has.

unmute ( )   Unmutes the sound.

Usage Web & Application
Related Minim