I’m writing a music player. When I read data from external music file I fill a buffer with amplitude information. Since I figured it would make sense to spawn this blocking action into it’s own thread I added an interface that runs it within runnable :
public class AudioInterface implements Runnable {
public void run()
{
AudioManager am = new AudioManager();
am.play("res/sample2.mp3");
}
}
Here are my following questions :
- How do I create a hook from another class to poll the amplitude data? I need it to write an algorithm that will be drawn onto a JPanel canvas.
- Should the panel that enables visualization also be in a different thread?
- Currently run() only includes playing the file, how would I use this thread for other actions such as posing and seeking etc since there is only one run action.
Thanks
Some thoughts:
Since this is a Swing program, consider using a SwingWorker for your background thread, and then pumping the amplitude data to the GUI portion of the program via SwingWorker’s publish/process method pair.
Careful here. All Swing code should be called on one thread and one thread only, the event dispatch thread. Please have a look at the tutorial on this, Concurrency in Swing.
You don’t use “threads” for this, you call methods of objects.
Also, you’ll likely want to declare your AudioManager variable as a class field, not a local variable, since otherwise its scope is limited to the method it was declared in, preventing other code from interacting with it.