简体   繁体   中英

Trying to play multiple, selected files in Java/Processing in order of selection

I'm trying to write Java code in which I can select multiple audio files, and have the script play the files in order of selection via a music visualiser.

I currently can select multiple files but only play the first one on a loop.

Here is my code:

import ddf.minim.*;
import ddf.minim.analysis.*;
import javax.swing.JFileChooser;
import processing.pdf.*;
PImage mouseCursor;
Minim         minim;
AudioPlayer   player;
AudioMetaData meta;
BeatDetect    beat;
FFT           fft;
int           w;
float         rad = 70;
int           r   = 2000;

void setup() {
  beginRecord(PDF, "/Users/Luke/Desktop/Processing Projects and     Files/f.pdf");
  fullScreen();  
  JFileChooser chooser = new JFileChooser();
  chooser.setMultiSelectionEnabled(true);
  chooser.setFileFilter(chooser.getAcceptAllFileFilter());
  //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  int returnVal = chooser.showOpenDialog(null);
  minim = new Minim(this);
  player =                 minim.loadFile(chooser.getSelectedFile().getAbsolutePath());
  meta = player.getMetaData();
  beat = new BeatDetect();
  player.loop();
  //player.isLooping();
  fft = new FFT(player.bufferSize(), player.sampleRate());
  fft.logAverages(60, 7);
  noStroke();
  w = width/fft.avgSize();
  background(0);
}
void draw() {
  noCursor();
  background(0);
  fill(255);
  fft.forward(player.mix);
  //stroke(255);
  stroke(random(255), random(255), random(255));
  for (int i = 0; i < fft.avgSize(); i++) {
    line((i * w) + (w / 2), height, (i * w) + (w / 2), height -                     fft.getAvg(i) * 4);
    strokeWeight(20);
  }
  beat.detect(player.mix);
  fill(0, 20);
  noStroke();
  rect(0, 0, width, height);
  //translate(width/1, height/50);
  noFill();
  fill(-1, 0);
  if (beat.isOnset()) rad = rad * 0.9f;
    else rad = 70;
  ellipse(0, 0, 2 * rad, 2 * rad);
  strokeCap(PROJECT);
  stroke(-1, 30);
  int bsize = player.bufferSize();
  for (int i = 0; i < bsize - 1; i += 5) {
    float x = 4 * (r)*cos(i*2*PI/bsize);
    float y =  4 * (r)*sin(i*2*PI/bsize);
    float x2 = 4 * (r + player.left.get(i)*100)*cos(i*2*PI/bsize);
    float y2 = 4 * (r + player.left.get(i)*100)*sin(i*2*PI/bsize);
    line(x, y, x2, y2);
  }
}
void keyPressed() {
  if (key==' ')exit();
  if (key=='s' || key=='S')endRecord();
}

At the moment it only plays one file and loops

Many thanks for the help

K

You need to break your problem down into smaller steps and only focus on one of those steps at a time.

For example, you should probably create a separate sketch that uses JFileChooser to allow the user to select multiple files. Don't worry about playing them yet, just print their names out to the console. You should read the Java API for useful functions.

When you get that working perfectly, then you can think about getting the click order. That's more advanced, but Google is your friend, and here is a question with a few promising answers. Again, you should do this in its own small example sketch. Don't worry about playing the audio files yet. Just print them to the console.

Separately from that, create a sketch that uses a hard-coded array or ArrayList of audio files and plays them in order. Get that working separately from the file selection logic.

When you get them all working by themselves, it'll be much easier to combine them into a single sketch. And if you get stuck on a specific step, then you can post a MCVE of just that step and we'll go from there. Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM