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