为什么我的程序不播放数组中的下一首歌曲?

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

我已经研究这段代码很长时间了,但我似乎无法弄清楚我的问题。我希望能够一个接一个地播放歌曲列表,我想我可以用一种简单的递归方法来做到这一点,这种方法可以延迟歌曲的平均长度,并让它调用下一首歌曲并播放它。 ..然而它只播放第一首歌然后在那之后停止并且没有其他任何事情发生......我已经让无数人看过这个但没有人能帮助我......不这不是学校项目,它是一个音乐播放器,我妈妈希望我在下一个即将到来的周末的聚会上使用它,所以这就像我最后的努力...任何对此的帮助将不胜感激!!!

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

}

}

看来您的代码失败的原因与此相同:

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++);
}

输出这个:

0
0
0
0
0

您的侦听器有一个 x 字段,您在方法之间 按值传递 。您应该删除 playMusic() 上的 x 参数,因此每次递增 x 时,它都会使用对象字段。