2016-06-11 48 views
-3

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

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

} 

}

+1

好的,我不会问你为什么你的母亲希望你使用这个而不是spotify ......反正......这个代码来自哪里?你有没有试过调试它?为什么你认为thread.sleep(5 * 60 * 1000)导致了这个问题? – Paolof76

+0

我认为这是造成这个问题的原因,因为在我添加之前,这个方法奏效了,但是这些歌曲会在另一个之上播放。所以我试图做的是将它们分开,所以一个会玩,然后下一个等等 – amich

回答

2

看来你的代码失败出于同样的原因是:

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时,它都会使用对象字段。

+0

使用playMusic(++ x); –

+0

没有,它没有帮助 – everton

+0

逻辑上在我的脑中应该每次打印1个。所以你说的是我需要先增加X然后调用方法? – amich

相关问题