2022-04-19
学习的几点:
-
调用函数库。sound函数库需要确认是否下载好。如无,则需要提前准备:
image.png
-
待播放的音乐文件准备。
这个通过加载就可以自动放到文件夹下的data子目录下。当然也可以自己建立data子目录并把音乐文件放到里面。
image.png
- 第一个例子。
import processing.sound.*;
SoundFile file;
void setup() {
size(640, 360);
background(255);
// Load a soundfile from the /data folder of the sketch and play it back
file = new SoundFile(this, "kb.mp3");
file.play();
}
void draw() {
}
本示例只是播放音乐,没有绘制任何内容。所以出来的窗口是空白的。
这里使用到一个函数库。利用其中方法生成一个对象并调用播放(play)方法即可。
- 对播放进行控制。如鼠标暂停等,参考如下:
为实现鼠标控制,增加了鼠标点击事件。
- 对播放进行控制。如鼠标暂停等,参考如下:
//引入processing的sound库
import processing.sound.*;
SoundFile song;
void setup(){
size(640, 360);
//初始化一个音频文件对象,音频文件为1.mp3,文件存放在当前*.pde文件所在目录的data文件夹下,同图片的存放路径
song =new SoundFile(this,"1.mp3");
}
void draw(){
}
//添加鼠标事件
void mousePressed(){//点击鼠标
if(song.isPlaying()){//如果声音在播放
song.pause();//暂停
}else{//否则
song.play();//播放声音
//play()函数只播放声音一次,如果要重复播放使用loop(),也就是song.loop()
}
}
- 控制音量。
import processing.sound.*;
SoundFile song;
void setup(){
size(200, 200);
song =new SoundFile(this,"1.mp3");
song.loop();
}
void draw(){
background(255);
float volume = map(mouseX, 0, width, 0, 1);
song.amp(volume);
}
改进版本如下:(安居正作品)
import processing.sound.*;
SoundFile song;
void setup()
{
size(640,360);
background(255);
song=new SoundFile(this,"kb.mp3");
song.loop();
}
void draw()
{
String str="Vol";
String str1="click to continue/stop the music";
text(str,250,180);
textSize(15);
fill(0);
text(str1,400,300);
textSize(30);
stroke(0);
strokeWeight(20);
line(320,360,320,mouseY);//show the volum directly by a line.
float vol=map(mouseY,height,0,0,1);
song.amp(vol);
stroke(255);
line(320,0,320,mouseY);//cover the old line with a same white line,which equals to delete the old one.
}
void mousePressed()
{
if(song.isPlaying())
{
song.pause();
}
else
{
song.play();
}
}
![](https://img.haomeiwen.com/i14351057/5842e86656bd2a97.png)
- 控制循环播放:
import processing.sound.*;
SoundFile file;
void setup() {
size(640, 360);
background(255);
// Load a soundfile from the data folder of the sketch and play it back in a loop
file = new SoundFile(this, "sample.mp3");
file.loop();
}
void draw() {
}
核心的是使用sound库。
SoundFile
-
This is a Soundfile Player which allows to play back and manipulate soundfiles.
-
Offset the output of the player by the given value.
-
Changes the amplitude/volume of the player.
-
Returns the number of channels of the soundfile as an int (1 for mono, 2 for stereo).
-
Cues the playhead to a fixed position in the soundfile.
-
Returns the duration of the soundfile in seconds.
-
Returns the number of frames of this soundfile.
-
Check whether this soundfile is currently playing.
-
Jump to a specific position in the soundfile while continuing to play (or starting to play if it wasn't playing already).
-
Starts playback which will loop at the end of the soundfile.
-
Move the sound in a stereo panorama.
-
Stop the playback of the file, but cue it to the current position.
-
Starts the playback of the soundfile.
-
Set the playback rate of the soundfile.
-
Remove this SoundFile's decoded audio sample from the cache, allowing it to be garbage collected once there are no more references to this SoundFile.
-
Set multiple playback parameters of the soundfile at once.
-
Stops the playback.
网友评论