Minim loadFile method problem
in
Core Library Questions
•
1 year ago
Hello forumers,
I have a really strange problem that I encountered with the Minim's loadFile method on my audio visualizer program.
My program does this:
It will first load a song into the applet to play and the applet will do animations using the data from the song.
I implemented a drag-and-drop feature, where when I drag a sound file (I used mp3's and wav's) to the applet, it will change the current song to the one I dropped.
Everything works smoothly when I'm running on the AppletViewer, but when I run the applet on a web browser (I've tested Chrome, Firefox, and IE), the moment I release my mouse and drop the file to the applet, the current song stops playing but the new song doesn't start.
Here's my code snippets:
Imports:
- import java.awt.datatransfer.DataFlavor;
- import java.awt.datatransfer.Transferable;
- import java.awt.dnd.DnDConstants;
- import java.awt.dnd.DropTarget;
- import java.awt.dnd.DropTargetDragEvent;
- import java.awt.dnd.DropTargetDropEvent;
- import java.awt.dnd.DropTargetEvent;
- import java.awt.dnd.DropTargetListener;
- import java.util.ArrayList;
- import java.util.Random;
- import processing.core.*;
- import ddf.minim.*;
- import ddf.minim.analysis.FFT;
Instances:
- AudioPlayer player;
- Minim minim;
- BeatListener b1;
- FFT fft;
- ArrayList<Circle> circles;
- int frameCounter;
- Random rand = new Random();
- DropTarget dt;
- String filename;
setup() method:
- public void setup(){
- //frameRate(60);
- size(1920,1080, P2D);
- noStroke();
- filename = "Nujabes - Beyond.mp3";
- minim = new Minim(this);
- player = minim.loadFile(filename);
- player.play();
- fft = new FFT(player.bufferSize(), player.sampleRate());
- fft.linAverages(128);
- dt = new DropTarget(this, this);
- setVisible(true);
- circles=new ArrayList<Circle>();
- for (int i = 0; i< 24; i++){
- int dx = 0;
- int dy = 0;
- /*
- if(rand.nextBoolean()){
- dx = (rand.nextInt(4));
- }
- else{
- dx = -1*(rand.nextInt(4));
- }
- if (rand.nextBoolean()){
- dy = (rand.nextInt(3)+1);
- }
- else{
- dy = -1*(rand.nextInt(3)+1);
- }*/
- circles.add(new Circle(70+i*80, height/2, dx, dy, 50, 0));
- }
- frameCounter = 0;
- }
DropTargetListener Methods:
- @Override
- public void dragEnter(DropTargetDragEvent dtde) {
- // TODO Auto-generated method stub
- System.out.println("Drag Enter");
- }
- @Override
- public void dragOver(DropTargetDragEvent dtde) {
- // TODO Auto-generated method stub
- System.out.println("Drag Over");
- }
- @Override
- public void dropActionChanged(DropTargetDragEvent dtde) {
- // TODO Auto-generated method stub
- System.out.println("Drag Action Changed");
- }
- @Override
- public void dragExit(DropTargetEvent dte) {
- // TODO Auto-generated method stub
- System.out.println("Source: " + dte.getSource());
- System.out.println("Drag Exit");
- }
- @Override
- public void drop(DropTargetDropEvent dtde) {
- try {
- Transferable tr = dtde.getTransferable();
- DataFlavor[] flavors = tr.getTransferDataFlavors();
- for (int i = 0; i < flavors.length; i++) {
- System.out.println("Possible flavor: " + flavors[i].getMimeType());
- if (flavors[i].isFlavorJavaFileListType()) {
- dtde.acceptDrop(DnDConstants.ACTION_COPY);
- String x = "";
- java.util.List list = (java.util.List) tr.getTransferData(flavors[i]);
- for (int j = 0; j < list.size(); j++) {
- x+=list.get(i);
- }
- if (x.endsWith(".mp3")||x.endsWith(".wav")||x.endsWith(".aiff")||
- x.endsWith(".au")||x.endsWith(".snd")){
- player.pause();
- player.close();
- minim.stop();
- player = minim.loadFile("http://envato.dasilveira.nl/html5-audioplayer-v1/mp3/track.mp3");
- filename = x;
- player.play();
- fft = new FFT(player.bufferSize(), player.sampleRate());
- fft.linAverages(128);
- dtde.dropComplete(true);
- }
- return;
- }
- }
- System.out.println("Drop failed: " + dtde);
- dtde.rejectDrop();
- } catch (Exception e) {
- e.printStackTrace();
- dtde.rejectDrop();
- }
- }
The problem occurs in line 46 of the DropTargetListener methods. I've debugged through this code and everything up until that line works fine. However, everything after I call the minim.loadFile("[some pathname]") method doesn't get executed. As you can see in that line, I tried to load a url sound file. Again, this works just fine in my appletviewer, but it doesn't work on the web browser.
I noticed that the first sound file (
"Nujabes - Beyond.mp3"), which is stored locally when compiled into the JAR, works fine in the web browser, but when I change that pathName to the same url or another pathname stored somewhere else on my computer, it just stops at that line of code too and doesn't execute anything after it.
I've concluded that when running on the web browser, somehow the method minim.loadFile([pathname]) is returning null, so it's giving me a NullPointerException. I've been looking through the original source on Minim's github (
https://github.com/ddf/Minim/blob/master/src/ddf/minim/Minim.java), and I've been trying to figure out why it's been returning null.
My PApplet can also be viewed here:
http://www.prism.gatech.edu/~jzhang300/AudioVisualizer.html
Note: the applet is very big (1920x1080 px)
Does anyone have any suggestions or solutions to this of how I can fix this issue? Any help would be much appreciated and thanks in advanced! :)
- James
1