这里有一些东西可以帮助你:
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
String filename;
void setup() {
textSize(24);
frame.setResizable(false);
background(255);
size(600, 200);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
} else {
filename = selection.getAbsolutePath();
player = minim.loadFile(filename);
player.play();
println("User selected " + filename);
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
这是一个非常简单的例子。所有这一切都是Minim
为. 然后,如果该文件是合法的音频文件(我只测试了一个文件),那么它就会播放它。完成后,播放器停止。AudioPlayer
loadFile(filename)
.wav
请记住,这不会进行任何错误检查或任何操作,因此如果您选择了一个.jpeg
文件,例如,它将引发异常。您应该使用这些东西来尝试看看如何简化您的播放器。
您应该尝试理解的一件事是,这一切都非常简单。文件名很简单String
,没有什么特别复杂的。
你可以在这里找到一个教程:http Minim
: //artandtech.aalto.fi/wp-content/uploads/2012/06/minim.pdf
更新:使用选定歌曲的正弦波
我已经更新了我在此处提供的代码,以处理您正在进行的波操作。我添加了一个在选择文件时boolean
变为true
的。现在您将不得不调整此代码以处理多个文件。这只是一个例子。
更新 2:实现了与您的歌曲类一起使用的代码
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;
void setup() {
s = new ArrayList();
textSize(24);
frame.setResizable(false);
background(255);
size(600, 600);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
if (isSelected) {
s.get(k).waveform();
}
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
}
else {
filename = selection.getAbsolutePath();
s.add(new Songs(player, filename, "Filename"));
isSelected = true;
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
class Songs {
AudioPlayer song;
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {
song=minim.loadFile(directory);
this.song=song;
this.songName=songName;
song.play();
}
void waveform() {
for (int j = 1; j < song.bufferSize() - 1; j++)
{
if (j>0) {
line(j, 214 + song.left.get(j)*50, j+1, 214 + song.left.get(j+1)*50);
//waves from the left.
stroke( 255, 0, 0 ); //this is the colour of the first line (red)
line(j, 214 + song.right.get(j)*50, j+1, 214 + song.right.get(j+1)*50);
//waves from the right.
stroke(255, 255, 0);
}
}
}
}
您看到红色闪烁然后没有设置代码的方式的原因是因为它读取waveform()
方法并for
非常快速地通过循环,然后继续进行其余操作,例如播放歌曲。draw()
像我做的那样把它放进去可以帮助你避免这种情况。