增加进口按钮,音乐播放器 [英] Adding import button to music player

查看:230
本文介绍了增加进口按钮,音乐播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 进口ddf.minim.spi *。
导入ddf.minim *。
进口ddf.minim.signals *。
导入ddf.minim.analysis *。
进口ddf.minim.effects *。
进口ddf.minim.ugens *。
布尔播放;
布尔重复;
时int k;
字符串的文件名; //
微量微量;
AudioPlayer球员;图片磷;
机甲米;
Importbt B:
ArrayList的<&歌曲GT; S;
INT I = 0;
无效设置(){
大小(600,400);
B =新Importbt();
M =新机甲();
P =新的图片();
p.Thepics();微量=新的微量(本);
S =新的ArrayList();
s.add(新曲(球员,SONG 1,SONG 1));
s.add(新曲(球员,宋2,歌曲2));
s.add(新曲(球员,SONG 3,SONG 3));
s.add(新曲(球员,SONG 4,SONG 4));
K = s.size() - 1;
}
无效的draw(){
背景(0);
p.getFunction();
}无效fileSelected(文件选择){
如果(选择== NULL){
}
其他{
文件名= selection.getAbsolutePath();
玩家= minim.loadFile(文件名); //加载文件
// s.add(K,新歌曲(播放器,文件名,一歌));
//s.get(K).playmusic();
s.add(新歌曲(播放器,文件名,ftyu));
s.get(K).waveform(); //函数从我的歌曲类
player.play(); //播放文件

至于我的课如果需要

 类歌曲{
AudioPlayer歌曲;
串目录下;
串SONGNAME;
歌曲(歌曲AudioPlayer,目录字符串,字符串SONGNAME){曲= minim.loadFile(目录);this.song =歌曲;
this.songName = SONGNAME;
}
空隙波形(){
对于(INT J = 1; J< song.bufferSize() - 1; J ++)
{
  如果(J&0){    线(J,214 + song.left.get(J)* 50,J + 1,214 + song.left.get第(j + 1)* 50);
    //从左边波。
    笔划(255,0,0); //这是第一行的颜色(红色)
    线(J,214 + song.right.get(J)* 50,J + 1,214 + song.right.get第(j + 1)* 50);
    //从右边波。
    笔划(255,255,0);  }
}


解决方案

下面的东西让你去:

 进口ddf.minim.spi *。
进口ddf.minim.signals *。
导入ddf.minim *。
导入ddf.minim.analysis *。
进口ddf.minim.ugens *。
进口ddf.minim.effects *。浮buttonX;
浮buttonY;
浮buttonW;
浮buttonH;微量微量;
AudioPlayer球员;字符串的文件名;无效设置(){  TEXTSIZE(24);  frame.setResizable(假);  背景(255);  尺寸(600,200);  (0)填写;
  行程(0);
  NOFILL();  buttonW = 200;
  buttonH = 50;
  buttonX =宽度 - 宽度/ 2 - buttonW / 2;
  buttonY =身高/ 2 - buttonH / 2;  //微量的东西
  微量=新的微量(本);
}无效的draw(){  背景(255);
  (0)填写;  rectMode(角);  矩形(buttonX,buttonY,buttonW,buttonH);  补(255);  textAlign设置(左);
  文本(导入文件,buttonX + 35,buttonY + 30);
}无效的mouseClicked(){
  如果(mouseX> buttonX和放大器;&安培; mouseX< buttonX + buttonW&放大器;&安培; mouseY的> buttonY和放大器;&安培; mouseY的< buttonY + buttonH){
    selectInput(导入音乐文件,fileSelected);
  }
}/ *从Processing.org拍摄* /
无效fileSelected(文件选择){
  如果(选择== NULL){
    的println(窗口被关闭或用户打取消);
  }其他{
    文件名= selection.getAbsolutePath();
    玩家= minim.loadFile(文件名);
    player.play();
    的println(用户选择+文件名);
  }
}//停止微量和播放器。
空隙停止(){
  player.close();
  minim.stop();
  super.stop();
}

这是一个很简单的例子。这样做只是为创建对象微量 AudioPlayer ,然后使用文件选择机构供给的路径和名称该文件以的loadFile(文件名)。那么,如果该文件是一个合法的音频文件(我只用 .WAV 文件进行测试),那么就会播放出来。它完成之后,播放机将停止。

请记住,这并不做任何错误检查或任何东西,所以如果你选择了 .JPEG 文件,例如,它会抛出异常。你应该用这些东西打,试图看你怎么可以的流线型的播放器。

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

您可以找到微量这里的教程:的 http://artandtech.aalto.fi/wp-content/uploads/2012/06/minim.pdf

更新:正弦波所选歌曲WORKING

我已经更新了我的code,我在这里提供的与你要去的浪潮东西的工作。我添加了一个布尔变成真正当选择一个文件。现在你将有调整这个code有多个文件的工作。这仅仅是一个例子。

更新2:实现了code与您SONGS工作类

 进口ddf.minim.spi *。
进口ddf.minim.signals *。
导入ddf.minim *。
导入ddf.minim.analysis *。
进口ddf.minim.ugens *。
进口ddf.minim.effects *。浮buttonX;
浮buttonY;
浮buttonW;
浮buttonH;微量微量;
AudioPlayer球员;
ArrayList的<&歌曲GT; S;
时int k;字符串的文件名;布尔isSelected = FALSE;无效设置(){  S =新的ArrayList();  TEXTSIZE(24);  frame.setResizable(假);  背景(255);  尺寸(600,600);  (0)填写;
  行程(0);
  NOFILL();  buttonW = 200;
  buttonH = 50;
  buttonX =宽度 - 宽度/ 2 - buttonW / 2;
  buttonY =身高/ 2 - buttonH / 2;  //微量的东西
  微量=新的微量(本);
}无效的draw(){  背景(255);
  (0)填写;  rectMode(角);  矩形(buttonX,buttonY,buttonW,buttonH);  补(255);  textAlign设置(左);
  文本(导入文件,buttonX + 35,buttonY + 30);  如果(isSelected){
    s.get(K).waveform();
  }
}无效的mouseClicked(){
  如果(mouseX> buttonX和放大器;&安培; mouseX< buttonX + buttonW&放大器;&安培; mouseY的> buttonY和放大器;&安培; mouseY的< buttonY + buttonH){
    selectInput(导入音乐文件,fileSelected);
  }
}/ *从Processing.org拍摄* /
无效fileSelected(文件选择){
  如果(选择== NULL){
    的println(窗口被关闭或用户打取消);
  }
  其他{
    文件名= selection.getAbsolutePath();
    s.add(新歌曲(播放器,文件名,文件名));
    isSelected = TRUE;
  }
}//停止微量和播放器。
空隙停止(){
  player.close();
  minim.stop();
  super.stop();
}歌曲类{
  AudioPlayer歌曲;
  串目录下;
  串SONGNAME;
  歌曲(歌曲AudioPlayer,目录字符串,字符串SONGNAME){    曲= minim.loadFile(目录);    this.song =歌曲;
    this.songName = SONGNAME;
    song.play();
  }
  空隙波形(){
    对于(INT J = 1; J< song.bufferSize() - 1; J ++)
    {
      如果(J&0){        线(J,214 + song.left.get(J)* 50,J + 1,214 + song.left.get第(j + 1)* 50);
        //从左边波。
        笔划(255,0,0); //这是第一行的颜色(红色)
        线(J,214 + song.right.get(J)* 50,J + 1,214 + song.right.get第(j + 1)* 50);
        //从右边波。
        笔划(255,255,0);
      }
    }
  }
}

您看到的红色再没有什么闪光,你有你的code设置方式的原因是因为它读波形()方法,并通过云在循环非常快,然后移动到作业的其余部分像演奏的乐曲。将它放在平局()喜欢我所做的帮助你避免这种情况。

so I'm trying to make it possible to make a functional import button, by this I mean i click the button, the file browser pops up and I can click a song then the player can play it. Just like any other music player. Here is a basic view of my code so far excluding the classes:

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

As for my class if its needed

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

  }
}

解决方案

Here's something to get you going:

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

This is a very simple example. All this does is creates objects for Minim and AudioPlayer and then uses the file selection mechanism to feed the path and name of the file to loadFile(filename). Then if the file is a legitimate audio file (I only tested with a .wav file) then it plays it. After it is done, the player is stopped.

Keep in mind that this does not do any error checking or anything so if you chose a .jpeg file, for example, it will throw exceptions. You should play with these things to try and see how you can streamline your player.

One thing you should try and understand is that this is all very straightforward. The filename is simply a String and nothing extremely complicated.

You can find a tutorial for Minim here: http://artandtech.aalto.fi/wp-content/uploads/2012/06/minim.pdf

UPDATE: SINE WAVES WORKING WITH THE SELECTED SONG

I have updated my code that I provided here to work with the wave thing that you have going. I added a boolean which becomes true when a file is selected. Now you will have to tweak this code to work with multiple files. This is just an example.

UPDATE 2: IMPLEMENTED THE CODE TO WORK WITH YOUR SONGS CLASS

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

The reason you see a flash of red and then nothing the way you had your code set up was because it reads the waveform() method and goes through the for loop really fast and then moves on to the rest of the operations like playing the song. Putting it in draw() like I've done helps you avoid that.

这篇关于增加进口按钮,音乐播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆