2022-05-27
Basic sound playback,P454
程序需要导入函数库,并实例化一个soundFile。
在setup中设置播放一次。
另外,设置鼠标事件:在播放中按下鼠标时就暂停。如果处于暂停状态时按下鼠标就开始播放。
文件格式的问题:
The type of sound files compatible with Processing is limited. Possible formats are wav, aiff, and mp3. If you want to use a sound file that is not stored in a compatible format, you could download a free audio editor, such as Audacity (http://audacity.sourceforge.net/) and convert the file. Once the sound is loaded, playing is easy.
If you want your sound to loop forever, call loop() instead.
song.loop();
A sound can be stopped with stop() or pause().
import processing.sound.*;
SoundFile song;
void setup() {
size(640, 360);
song = new SoundFile(this, "song.mp3");
song.play();
}
void draw() {
}
void mousePressed() {
if (song.isPlaying()) {
song.pause();
} else {
song.play();
}
}
网友评论