2

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

}

Community
  • 1
  • 1
compski
  • 709
  • 3
  • 13
  • 28

3 Answers3

2

I had to deal with a very similar problem, and posted it here:

mark/reset exception during getAudioInputStream()

This form: .getResourceAsStream(fileName) returns an InputStream which throws a mark/reset exception if the file is not markable. The explanation I got is that there used to be a default "first guess" of .wav, but this is no longer the first guess (as of Java 7). There is a better, fuller description at Oracle's bug database for #7095006.

Use this form and you should be okay, because it doesn't require the intermediate step (InputStream) that needs to support marking & resetting:

URL url = AudioMixer.class.getResource(fileName); 
AudioInputStream ais =  AudioSystem.getAudioInputStream(url);  
Community
  • 1
  • 1
Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
1

In the linked question, the underlying base data stream is constructed a bit differently, so you have to adapt the solution.

Instead of this:

InputStream audioSrc = getClass().getResourceAsStream("2MB_sound.wav");

Use this:

InputStream audioSrc = new FileInputStream("85046_newgrounds_parago.wav");
Kevin K
  • 9,344
  • 3
  • 37
  • 62
  • thx for you answer AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn); however now this code has a problem with the same saying "java.io.IOException: mark/reset not supported" – compski Feb 17 '12 at 04:30
0

This code compiles.

import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;

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 {

    backgroundPlayer bp = new backgroundPlayer();

    public final void actionPerformed (ActionEvent e) {
        bp.music();
    }
}

public void music () {
    try {
    InputStream audioSrc = getClass().
        getResourceAsStream("85046_newgrounds_parago.wav");
    InputStream bufferedIn = new BufferedInputStream(audioSrc);
    AudioInputStream audioStream =
        AudioSystem.getAudioInputStream(bufferedIn);

    Clip clip = AudioSystem.getClip();
    clip.open(audioStream);
    clip.loop(Clip.LOOP_CONTINUOUSLY);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433