To play sounds along with slides in the
SlideShowFrame
, you would make the following modifications to the code:
private Clip soundClip[] = new Clip[NIMGS];
private Clip currentClip = null;
Declare an array of
URL
s to store the URLs of the audio files you want to play. Assign Clips to the array at the same time you input the images:
for (int k=0; k < NIMGS; k++) {
url =
new URL( "http://www.cs.trincoll.edu/~ram/jjj/slide" +
k + ".gif");
slide[k] = imageIO.read( url );
URL soundURL =
new URL("http://www.cs.trincoll.edu/~ram/jjj/sound" +
k + ".au");
AudioInputStream audio =
AudioSystem.getAudioInputStream(url);
DataLine.Info info = new DataLine.Info(Clip.class,
audio.getFormat());
soundClip[k] = (Clip) AudioSystem.getLine(info);
}
Change the nextSlide() code to the following
public void nextSlide() {
currentClip.stop(); // stop sound playback
currentClip = soundClip[nextImg]; // get next soundClip
currentClip.setFramePosition(0); // start clip at beginning
currentImage = slide[nextImg];
nextImg = ( nextImg + 1) % NIMGS;
repaint ();
}
Each time an image is displayed in
paint()
, play the corresponding sound by using the URL from the array:
public void paint(Graphics g) {
if (currentImage != null) {
g.drawImage(currentImage,10,10,this);
currentClip.start();
}
}