跳轨方式

Track skipping method

曲目播放没有问题。每当我单击下一首按钮时,它都会停止播放第一首歌曲,然后播放下一首随机歌曲。但缺陷是它没有暂停第二首歌。而且在第二首歌曲之后,每当我点击 NEXT 按钮时,它不会停止正在播放的歌曲并同时播放另一首歌曲。

我尝试冲洗剪辑并关闭它。此代码是当前版本。

public class Version_6 {

static File[] songs = new File[5];
public static int currentSongIndex;

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    songs[0] = new File("LoverBoi.WAV");
    songs[1] = new File("Wing.WAV");
    songs[2] = new File("rip.WAV");
    songs[3] = new File("rainOnMe.WAV");
    songs[4] = new File("wm.WAV");

    // playSong(songs[getRandomIntegerBetweenRange(0, 4)]);
    try {
        JFrame jf = new JFrame();
        jf.setState(Frame.MAXIMIZED_BOTH);
        jf.setVisible(true);

        jf.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("background.jpg")))));

        jf.pack();
        JButton button_start = new JButton("Start");
        JButton button_pause = new JButton("Pause");
        JButton btnNext = new JButton("Next");
        Container pane = jf.getContentPane();
        pane.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(button_start);
        pane.add(button_pause);
        pane.add(btnNext);
        jf.setBounds(0, 0, 300, 300);
        pane.setBounds(0, 0, 200, 200);
        button_start.setBounds(750, 450, 100, 100);
        button_start.setBounds(550, 250, 100, 100);
        btnNext.setBounds(350, 50, 100, 100);
        button_start.setVisible(false);
        btnNext.setVisible(true);

        AudioInputStream audioInput = AudioSystem.getAudioInputStream(songs[getRandomIntegerBetweenRange(0, 4)]);
        Clip clip;
        clip = AudioSystem.getClip();
        clip.open(audioInput);
        clip.start();
        clip.loop(clip.LOOP_CONTINUOUSLY);
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                {
                    clip.stop();
                    clip.flush();
                    clip.drain();
                    clip.close();
                    if (clip.isRunning() == true) {

                    }

                    nextSong(clip, getRandomIntegerBetweenRange(0, 4));
                }
            }
        });
        button_start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pauseAndContinue(clip);
                button_start.setVisible(false);
                button_pause.setVisible(true);
            }
        });
        button_pause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e1) {
                pauseAndContinue(clip);
                button_start.setVisible(true);
                button_pause.setVisible(false);

            }
        });
    } catch (Exception e) {
    }

}

/**
 * @param clip
 *            currently loaded clip This method is for playing and pausing
 *            the clip
 */
public static void pauseAndContinue(Clip clip) {
    if (clip.isRunning() == true) {
        clip.stop();

    } else if (clip.isRunning() == false) {
        clip.start();
        // clip.loop(clip.LOOP_CONTINUOUSLY);
    }
}

/**
 * @param chosenClip
 *            clip that is playing right now (or should i say "loaded right
 *            now")
 * @param currentSongIndex
 *            Array index of current opened file
 */
public static void nextSong(Clip chosenClip, int currentSongIndex) {
    if (chosenClip.isRunning() == true) {
        chosenClip.stop();
        chosenClip.flush();
        chosenClip.close();
    }
    if (currentSongIndex + 1 > 4)
        currentSongIndex = 0;
    currentSongIndex = currentSongIndex + 1;
    playSong(songs[currentSongIndex]);

}

/**
 * @param songFile
 *            chosen song file This method is for playing the song
 */
public static void playSong(File songFile) {
    AudioInputStream audioInput;
    try {
        audioInput = AudioSystem.getAudioInputStream(songFile);
        Clip clip;
        clip = AudioSystem.getClip();
        clip.open(audioInput);
        clip.start();
        // clip.loop(clip.LOOP_CONTINUOUSLY);
    } catch (Exception e) {
    }
}

}

我希望每当我点击 NEXT 按钮时它会播放下一首歌曲,当播放完最后一首歌曲时,它会再次从头开始。 提前致谢。

发生这种情况是因为您正在为剪辑创建局部变量,然后试图停止错误的剪辑。

首先,在 class 的顶部创建一个静态 Clip 变量:

static File[] songs = new File[5];
public static int currentSongIndex;
static Clip clip;

接下来,确保在 playSong 方法中引用静态 Clip 变量,方法是使用 this.clip 而不是 Clip clip:

public static void playSong(File songFile) {
    AudioInputStream audioInput;
    try {
        audioInput = AudioSystem.getAudioInputStream(songFile);
        //Deleted this line:
        //Clip clip;
        //This line has been updated:
        this.clip = AudioSystem.getClip();
        clip.open(audioInput);
        clip.start();
    } catch (Exception e) {
    }
}

并且在您的主要方法中:

    AudioInputStream audioInput = AudioSystem.getAudioInputStream(songs[getRandomIntegerBetweenRange(0, 4)]);
    //Deleted this line:
    //Clip clip;
    //Changed this line:
    this.clip = AudioSystem.getClip();
    clip.open(audioInput);
    clip.start();
    clip.loop(clip.LOOP_CONTINUOUSLY);

现在它应该可以正常工作了。