Why does going back and foward with audio clips just stop them working?

1 day ago 3
ARTICLE AD BOX

So I am trying to make an audio player just because I want to understand stacks and GUIs in java better, I was expecting when a audio file was chosen it should have played which it did before but it doesn't now, I have gone back to the past code to see the difference but there wasn't nothing I saw as a difference that should have stopped it from working, I haven't got any errors in the terminal either.

I have tried going through the java se 8 docs and some youtube videos about audio clips in java and all seemed right to what I already knew not sure what the issue, also I have remove current = numberofClips; from the code as a test and only one of the 2 audio files actually played for some reason.

here is a reproducible version of the code:

import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.io.File; import java.util.Stack; import javax.imageio.ImageIO; import javax.sound.sampled.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class MusicPlayerGui extends JFrame implements ActionListener { JMenu songMenu; JMenuItem loadMusic; JMenuItem loadPlaylist; // Button JPanel playbackBtns; JButton prevButton; JButton playButton; JButton pauseButton; JButton nextButton; // Music stuff Stack<Clip> clips = new Stack<>(); int next; int prev; int current = 0; float frameRate; int numberofClips; boolean play = true; boolean first = true; boolean playlist; public MusicPlayerGui() { super("Music Player"); setSize(400, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); setLayout(null); getContentPane().setBackground(BG_Color); // Ignore this addUiComponents(); } private void addUiComponents() { addToolbar(); addPlaybackBtns(); // removed the rest of the function } private void addToolbar() { toolbar = new JToolBar(); toolbar.setBounds(0, 0, getWidth(), 30); toolbar.setFloatable(false); add(toolbar); menuBar = new JMenuBar(); toolbar.add(menuBar); songMenu = new JMenu("Song"); menuBar.add(songMenu); loadMusic = new JMenuItem("Load music from directory"); loadMusic.addActionListener(this); songMenu.add(loadMusic); loadPlaylist = new JMenuItem("Load playlist from directory"); loadPlaylist.addActionListener(this); songMenu.add(loadPlaylist); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loadMusic) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("Music Files", "wav")); int response = fileChooser.showOpenDialog(null); if (response == JFileChooser.APPROVE_OPTION) { File file = new File(fileChooser.getSelectedFile().getAbsolutePath()); try (AudioInputStream aS = AudioSystem.getAudioInputStream(file)) { if (play == true) { enablePlayButton(); clips.get(current).stop(); } numberofClips++; Clip clip = AudioSystem.getClip(); current = numberofClips; clip.open(aS); clips.push(clip); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) { System.out.println(ex); } } } if (e.getSource() == loadPlaylist) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int response = fileChooser.showOpenDialog(null); if (response == JFileChooser.APPROVE_OPTION) { File files = new File(fileChooser.getSelectedFile().getAbsolutePath()); for (File file : files.listFiles()) { try (AudioInputStream aS = AudioSystem.getAudioInputStream(file)) { if (play == true) { enablePlayButton(); clips.get(current).stop(); } numberofClips++; Clip clip = AudioSystem.getClip(); playlist = true; clip.open(aS); clips.push(clip); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) { System.out.println(ex); } } } } if (e.getSource() == playButton) { enablePauseButton(); clips.get(current).start(); next = current + 1; prev = current - 1; if (playlist) { if ((clips.empty() && clips.get(current).isRunning() != false) && next > numberofClips) { enablePauseButton(); clips.get(next).start(); current = next; } } } if (e.getSource() == pauseButton) { enablePlayButton(); clips.get(current).stop(); } if (e.getSource() == prevButton) { if (first != true && prev < 0) { enablePlayButton(); clips.get(current).stop(); clips.get(prev).start(); current = prev; next = current + 1; prev = current - 1; } } } }

Also here is the main file so it can be run:

import javax.swing.SwingUtilities; public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MusicPlayerGui().setVisible(true);; } }); } }
Read Entire Article