Manual: Playable

[ javadoc | examples ]

The Playable interface defines functionality that you would expect from a tape deck or CD player. Implementing classes are usually playing an audio file. Playable is implemented by AudioPlayer and AudioSnippet.

Playing and Pausing

When you have a Playable object you can start and stop playback by using play() and pause(). When you use play() the sound will begin playing from the current position. If you’ve just loaded the file, it will start from the beginning. If it was playing for a while and then you paused it, it will begin playing from where it was paused. You can use isPlaying() to find out whether or not a sound is currently playing. As of Minim 1.1, when a sound finishes playing and has not been set to loop (see below), isPlaying() will continue to return true until pause() is called. This inconsistency will be fixed in the next release.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/isPlaying/isPlaying.pde[/snip]

Rewinding

Calling rewind() on a Playable object resets the “playhead” to the beginning of the sound. Rewinding doesn’t change the play state of a Playable so if it is playing or looping when you rewind, it will continue to play or loop after you rewind it.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/rewind/rewind.pde[/snip]

Looping

Often you want a sound to loop for a given number of times or perhaps indefinitely. This can be accomplished by using the loop methods:

[snip java]
void loop()
void loop(int numLoops)
[/snip]

Calling the version with no arguments will make the sound loop indefinitely, while calling the version with an int will loop the sound the specified number of times. It should be noted that looping the sound one time will cause the sound to be played twice, looping the sound two times will cause it to be played three times and so on. You can find out whether or not a sound has been set to loop by calling isLooping(). This will be true as long as there are loops left to be played, even if you pause the sound. To find out how many loops are left to be performed, you can call loopCount(). If you call play() before the sound has looped the number of times you’ve asked for, it will cancel out the looping behaviour and play to the end of the sound and stop. This can be useful if you want the sound to stop looping but don’t want the sound to cut off in the middle.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/loopNum/loopNum.pde[/snip]

Loop Points

It is possible to loop only a portion of a Playable object by calling setLoopPoints(int start, int stop). start and stop are times expressed in milliseconds. This method will work best on AudioSnippets because the audio is already loaded into memory and seeking to a particular point is very fast. However, you will probably find that using this with an AudioPlayer will have noticable lag, especially if the start time is far into the file. The reason for this is because every time the loop restarts the file must be seeked through from the beginning. This is particularly slow with mp3 files because of the time spent unencoding the samples being skipped over.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/setLoopPoints/setLoopPoints.pde[/snip]

Cueing

When you cue, it is always measured from the beginning of the recording. So cue(100) will set the “playhead” at 100 milliseconds from the beginning no matter where it currently is. Cueing a Playable object will not change the playstate, meaning that if it was already playing it will continue playing from the cue point, but if it was not playing, cueing will not start playback, it will simply set the point at which playback will begin. If an error occurs while trying to cue, the position will not change. If you try to cue to a negative position or try to cue past the end of the recording, the amount will be clamped to zero or length().

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/cue/cue.pde[/snip]

Skipping

When you skip, it is always measured from the current position of the recording. So skip(100) will set the “playhead” at 100 milliseconds from the current position. A sort of fast-forward. It is also possible to skip in a negative direction. So skip(-200) will set the “playhead” to 200 milliseconds before the current position. Using skip will not change the playstate of a Playable, meaning that if it was already playing it will continue playing from the new position, but if it was not playing, skipping will not start playback, it will simply set the point at which playback will begin. If an error occurs while trying to skip, the position will not change. If you try to skip to a position that is less than zero or try to skip past the end of the recording, the position will be clamped to zero or length().

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/skip/skip.pde[/snip]

Position and Length

The position method of a Playable returns the current position of the “playhead” in milliseconds. In other words, it’s how much of the recording has been played. The length method returns how long the Playable is in milliseconds (1 ms = 1/1000 of a second). The example below demonstrates how you could use the position and length methods to visualize where in the recording the “playhead” is.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/position/position.pde[/snip]

AudioMetaData

Audio files will often have metadata associated with them, such as the song name, duration, and so forth. The most familiar form of metadata is probably ID3 tags, used by mp3 files. Once you load up an audio file, you can get an AudioMetaData object from it by calling getMetaData(). With that object you can then call the following methods:

[snip java]
String album()
String author()
String comment()
String composer()
String copyright()
String date()
String disc()
String encoded()
String fileName()
String genre()
int length()
String orchestra()
String publisher()
String title()
int track()
[/snip]

If a piece of information is not available, the method will return either an empty string or -1, depending on the return type.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/Playable/getMetaData/getMetaData.pde[/snip]

9 thoughts on “Manual: Playable

  1. Pingback: C:Art:Media testsite · automatic bai chans

  2. I am trying to make a short wav file loop infinitely over and over again. When I run this code

    //—————————————————————-

    import ddf.minim.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;

    Minim minim;
    AudioPlayer player;

    minim = new Minim(this);
    player = minim.loadFile(“A.WAV”, 2048);

    player.loop();
    //—————————————————————————-

    I keep receiving the error

    ==== JavaSound Minim Error ====
    ==== Couldn’t rewind!

  3. That just means it encountered some error when trying to rewind your file. Something to do with the format of the file, I imagine. However, this does highlight the fact that I should probably include the text from the exception in this error message. Gonna do it right now!

  4. I would like to play a small mp3 file once depending on a variable threshold. However when i run the following code it plays the file several times then stops.

    if(value3 40){
    player2.rewind();
    }

  5. It seems I have to put in a delay after rewinding before pausing and then restarting (apparently to clear some buffer) otherwise I get a little of the song where I just paused it. Is there a better way of doing this, or is it just inherent?

    Thanks!
    –Frank

    void mousePressed()
    {
    if (start) {
    song.rewind();
    delay(250);
    song.pause();
    start = !start;
    } else {
    song.play();
    start = !start;
    }
    }

  6. Can i scramble audio forward and backward with a mouse scroll. Or just to make it simple the up and down key on the keyboard?

Comments are closed.