增加进口按钮,音乐播放器(Adding import button to music player)

2019-10-19 16:29发布

我想有可能使一个功能导入按钮,我的意思是我按一下按钮,文件浏览器弹出,我可以点击一首歌曲,然后播放器可以播放它。 就像任何其他的音乐播放器。 这里是我的代码,到目前为止不含类基本观点:

import ddf.minim.spi.*;
import ddf.minim.*; 
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.ugens.*;
boolean play;
boolean repeat;
int k;
String filename;//
Minim minim;
AudioPlayer player;

Pics p;
Mechs m;
Importbt b;
ArrayList<Songs> s;
int i=0;
void setup() {
size(600, 400);
b=new Importbt();
m=new Mechs();
p=new Pics();
p.Thepics();

minim=new Minim(this);
s = new ArrayList();
s.add(new Songs(player, "SONG 1", "SONG 1"));
s.add(new Songs(player, "SONG 2", "SONG 2"));
s.add(new Songs(player, "SONG 3", "SONG 3"));
s.add(new Songs(player, "SONG 4", "SONG 4"));
k = s.size()-1;
}
void draw() {
background(0);
p.getFunction();
}

void fileSelected(File selection) {
if (selection == null) {
} 
else {
filename = selection.getAbsolutePath();
player = minim.loadFile(filename);//loads the file
// s.add(k, new Songs(player, filename, "a song"));
//s.get(k).playmusic();
s.add(new Songs(player, filename, "ftyu"));
s.get(k).waveform();//function from my songs class
player.play();//plays the file

至于我的课,如果它需要

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;
}
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);

  }
}

Answer 1:

这里的东西让你去:

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();
}

这是一个非常简单的例子。 这一切都为创建对象的MinimAudioPlayer ,然后使用文件选择机制的文件的路径和名称喂loadFile(filename) 。 那么,如果该文件是一个合法的音频文件(我只用一个测试.wav文件),然后它播放。 它完成后,玩家将停止。

请记住,这并不做任何错误检查或任何东西,所以如果你选择了.jpeg文件,例如,它会抛出异常。 你应该用这些东西打试试,看你如何简化您的播放器。

有一件事你应该尝试和明白的是,这一切都是非常简单的。 文件名是一个简单的String并没有什么非常复杂。

你可以找到一个教程Minim这里: http://artandtech.aalto.fi/wp-content/uploads/2012/06/minim.pdf

UPDATE:正弦波与所选诗歌来工作

我已经更新了我的代码,我在这里提供与你要去波东西的工作。 我添加了一个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()像我做可以帮助你避免这种情况。



文章来源: Adding import button to music player