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)
/** * This sketch demonstrates how to use the <code>play</code> method of a <code>Playable</code> class. * The class used here is <code>AudioPlayer</code>, but you can also play an <code>AudioSnippet</code>. * Playing a <code>Playable</code> causes it to begin playing from the current position. When it reaches * the end of the recording it will emit silence, it will not stop! In other words, if you play something and * it gets to the end of the file, it will not stop and rewind, it will continue to try to read the file, but get * nothing and send silence to the audio system. If you call <code>isPlaying()</code> at that point, it will return true, * because the player is still trying to read the file, think of a record player that gets to the end of a record. * It just goes around on the same groove. It's not making any sound (well, crackles maybe) but it is still playing. * Press 'p' to play the file. * */ import ddf.minim.*; Minim minim; AudioPlayer groove; WaveformRenderer waveform; void setup() { size(512, 200, P3D); minim = new Minim(this); groove = minim.loadFile("groove.mp3", 2048); waveform = new WaveformRenderer(); // see the example Recordable >> addListener for more about this groove.addListener(waveform); } void draw() { background(0); // see waveform.pde for an explanation of how this works 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.
I’ve been using this class but, when I play a sound until the end, it doesn’t play the last second or so.
Is this normal?
I’m just doing this:
AudioPlayer message = minim.loadFile(IMThread.path_incoming+”/”+msg, 2048);
message.play();
Thread.sleep(message.getMetaData().length());
I suppose it’s possible that the length returned by the metadata is just incorrect. You try using the length() method of AudioPlayer itself, which may work better. I haven’t looked at the code in a little while, I but I think if you are playing the mp3, the length in the metadata is pulled from the tags, whereas if it’s a wave file message.getMetaData().length() should be the same as message.length().
how do you make to do a loop with a sound?
when it finsih, i want that automatically restart
By calling the loop method of AudioPlayer.
Pingback: Minim Sound abspielen « processing – tutorial