amich amich - 5 months ago 15
Java Question

Why will my program not play the next song in the array?

I have been working on this code for a really long time and i just can't seem to figure out my problem. i want to be able to play a list of songs one right after another and i thought i would be able to do that with a simple recursive method that delays the average length of a song, and have it call the next song and play it... However it only plays the very first song and then stops after that and nothing else happens... I have asked countless people to look at this and nobody can help me out.. And no this is not a school project, it is a music player that my mother would like me to use at a party in the next upcoming weekend, so this is like my last ditch effort... Any help with this would be greatly appreciated!!!

private JLabel messageLabel;
private JButton playlist;
private JPanel panel;
BufferedImage image;
AudioStream audioStream1, audioStream2, audioStream3;
//Object[] music = new Object[3];
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 525;

// File destinationss
private String s1 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\No_Pressure.wav";
private String s2 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\Grateful_Dead_-_Touch_of_Grey.wav";
private String s3 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\Stairway_to_Heaven_Led_Zeppelin_Lyrics.wav";

InputStream in1 = new FileInputStream(s1);
InputStream in2 = new FileInputStream(s2);
InputStream in3 = new FileInputStream(s3);
private ArrayList music;

public JukeBoxWithArrays() throws IOException {

music = new ArrayList();

audioStream1 = new AudioStream(in1);
audioStream2 = new AudioStream(in2);
audioStream3 = new AudioStream(in3);

music.add(audioStream1);
music.add(audioStream2);
music.add(audioStream3);

setTitle("Juke Box Playlist");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageLabel = new JLabel("Click the Button to play the playlist");

// Create the Playlist button
playlist = new JButton("Playlist number 1");

// Register the event Listener
playlist.addActionListener(new PlaylistListener());

// Create the panel
panel = new JPanel();
image = ImageIO.read(new File("C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\jukebox2.jpg"));
panel.add(messageLabel);
panel.add(playlist);
panel.add((new JLabel(new ImageIcon(image))));

// Add the panel to the Content Pane
add(panel);

// Display the Window
setVisible(true);
}

private class PlaylistListener implements ActionListener {

int x = 0;

public void actionPerformed(ActionEvent e) {

try {
playMusic(x);

} catch (InterruptedException ex) {
Logger.getLogger(JukeBoxWithArrays.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void playMusic(int x) throws InterruptedException {

if (x > music.size()) {
AudioPlayer.player.stop((InputStream) music.get(x));

} else {
AudioPlayer.player.start((InputStream) music.get(x));

}
Thread.sleep(5 * 60 * 1000); // I believe this is where I am running into my problem
playMusic(x++);

}

}

@SuppressWarnings("restriction")
public static void main(String[] args) throws Exception {

JukeBoxWithArrays jbwa = new JukeBoxWithArrays();
jbwa.pack();

}


}

Answer

It seems your code is failing for the same reason this:

private static int x = 0;

public static void main(String[] args) throws ParseException {
    int x = 0;
    doSomething(x);
    doSomething(x);
    doSomething(x);
    doSomething(x);
    doSomething(x);
}

private static void doSomething(int x) {
    System.out.println(x++);
}

Outputs this:

0
0
0
0
0

Your Listener has an x field, that your are passing by value between the methods. You should remove the x argument on playMusic(), so everytime it increments x, it would use the object field instead.

Comments