O
OscarC
Hi!
I'm building a slideshow app and I'd like to be able
to play some music while viewing the photos. So, I thought
in using two separate threads, one for the slideshow and
another for playing the music (I'm using javazoom.net's
MP3 libraries). Something like this:
Thread t1 = new Thread(new Slideshow(album));
t1.start();
Thread t2 = new Thread(new MP3Play());
t2.start();
The t1 thread opens a new undecorated JFrame and shows
the photos one after another. The t2 threads starts
playing some MP3 file.
Using this I'm not getting what I want. Now, the music
plays AFTER viewing the photos, not at the same time
which is the purpose of using threads. What am I doing
wrong ?
The code for the MP3Play class is:
public class MusicPlayer implements Runnable, BasicPlayerListener {
public void run() {
// Instantiate BasicPlayer.
BasicPlayer player = new BasicPlayer();
// BasicPlayer is a BasicController.
BasicController control = (BasicController) player;
// Register BasicPlayerTest to BasicPlayerListener events.
// It means that this object will be notified on BasicPlayer
// events such as : opened(...), progress(...), stateUpdated(...)
player.addBasicPlayerListener(this);
try { // Open file, or URL or Stream (shoutcast, icecast) to play.
control.open(new File("file.mp3"));
// control.open(new URL("http://yourshoutcastserver.com:8000"));
// Start playback in a thread. control.play();
// If you want to pause/resume/pause the played file then
// write a Swing player and just call control.pause(),
// control.resume() or control.stop().
// Use control.seek(bytesToSkip) to seek file
// (i.e. fast forward and rewind). seek feature will
// work only if underlying JavaSound SPI implements
// skip(...). True for MP3SPI (JavaZOOM) and SUN SPI's
// (WAVE, AU, AIFF).
// Set Volume (0 to 1.0). control.setGain(0.85);
// Set Pan (-1.0 to 1.0).
control.setPan(0.0);
control.play();
} catch (BasicPlayerException e) { e.printStackTrace(); }
}
public void stateUpdated(BasicPlayerEvent event) {
// Notification of BasicPlayer states (opened, playing, end of
media, ...)
System.out.println("stateUpdated : "+event.toString());
}
public void progress(int bytesread, long microseconds, byte[]
pcmdata, Map properties) {
// Pay attention to properties. It depends on underlying JavaSound SPI
// MP3SPI provides mp3.equalizer.
System.out.println("progress : "+properties.toString());
}
public void opened(Object stream, Map properties) {
// Pay attention to properties. It's useful to get duration,
// bitrate, channels, even tag such as ID3v2.
System.out.println("opened : "+properties.toString());
}
public void setController(BasicController controller) {
System.out.println("setController : "+controller);
}
}
Thanks in advance
I'm building a slideshow app and I'd like to be able
to play some music while viewing the photos. So, I thought
in using two separate threads, one for the slideshow and
another for playing the music (I'm using javazoom.net's
MP3 libraries). Something like this:
Thread t1 = new Thread(new Slideshow(album));
t1.start();
Thread t2 = new Thread(new MP3Play());
t2.start();
The t1 thread opens a new undecorated JFrame and shows
the photos one after another. The t2 threads starts
playing some MP3 file.
Using this I'm not getting what I want. Now, the music
plays AFTER viewing the photos, not at the same time
which is the purpose of using threads. What am I doing
wrong ?
The code for the MP3Play class is:
public class MusicPlayer implements Runnable, BasicPlayerListener {
public void run() {
// Instantiate BasicPlayer.
BasicPlayer player = new BasicPlayer();
// BasicPlayer is a BasicController.
BasicController control = (BasicController) player;
// Register BasicPlayerTest to BasicPlayerListener events.
// It means that this object will be notified on BasicPlayer
// events such as : opened(...), progress(...), stateUpdated(...)
player.addBasicPlayerListener(this);
try { // Open file, or URL or Stream (shoutcast, icecast) to play.
control.open(new File("file.mp3"));
// control.open(new URL("http://yourshoutcastserver.com:8000"));
// Start playback in a thread. control.play();
// If you want to pause/resume/pause the played file then
// write a Swing player and just call control.pause(),
// control.resume() or control.stop().
// Use control.seek(bytesToSkip) to seek file
// (i.e. fast forward and rewind). seek feature will
// work only if underlying JavaSound SPI implements
// skip(...). True for MP3SPI (JavaZOOM) and SUN SPI's
// (WAVE, AU, AIFF).
// Set Volume (0 to 1.0). control.setGain(0.85);
// Set Pan (-1.0 to 1.0).
control.setPan(0.0);
control.play();
} catch (BasicPlayerException e) { e.printStackTrace(); }
}
public void stateUpdated(BasicPlayerEvent event) {
// Notification of BasicPlayer states (opened, playing, end of
media, ...)
System.out.println("stateUpdated : "+event.toString());
}
public void progress(int bytesread, long microseconds, byte[]
pcmdata, Map properties) {
// Pay attention to properties. It depends on underlying JavaSound SPI
// MP3SPI provides mp3.equalizer.
System.out.println("progress : "+properties.toString());
}
public void opened(Object stream, Map properties) {
// Pay attention to properties. It's useful to get duration,
// bitrate, channels, even tag such as ID3v2.
System.out.println("opened : "+properties.toString());
}
public void setController(BasicController controller) {
System.out.println("setController : "+controller);
}
}
Thanks in advance