So I'm having kind of the same problem as java.io.IOException: mark/reset not supported.
How I want it to work:
- Make the program open a pop up button saying "Click me to play"
- Once clicked with cursor will play the 2MB_sound.wav (yes its 2MB in size) forever
What the problem is:
Somehow the code I wrote called backgroundPlayer totally works fine on one of the desktop in my uni comps but not in my laptop. When running the code on my laptop, the pop-up button works but when I click it ... it gives the error "java.io.IOException: mark/reset not supported".
What I have done to try resolve the problem but failed (from the link above's answer):
InputStream audioSrc = getClass().getResourceAsStream("2MB_sound.wav");
InputStream bufferedIn = new BufferedInputStream(audioSrc);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);
I tried adding that the code exactly as above (with the relevant imports) but it gave me a different error saying "Cannot make a static reference to the non-static method getClass() from the type Object". So now I'm stuck and went back to my original code as posted below.
Please help me resolve my issue.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class backgroundPlayer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200,200);
JButton button = new JButton("Click me to play");
frame.add(button);
button.addActionListener(new AL());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class AL implements ActionListener {
public final void actionPerformed (ActionEvent e) {
music();
}
}
public static void music () {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new FileInputStream("85046_newgrounds_parago.wav"));
clip.open(inputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}