所以我遇到類似於java.io.IOException: mark/reset not supported的問題。java.io.IOException:標記/重置不支持(靜態)
如何我希望它的工作:
- 使程序打開一個彈出按鈕,說:「點擊這裏給我玩」
- 一旦用光標點擊將扮演2MB_sound.wav(是其在2MB大小)永遠
什麼問題是:
不知怎的,我寫代碼調用backgroundPlayer完全工作正常,在桌面上的一個在我的單譜曲,但不是我的筆記本電腦。在我的筆記本電腦上運行代碼時,彈出按鈕可以正常工作,但是當我點擊它時,它會給出錯誤「java.io.IOException:mark/reset not supported」。
我做了什麼,試圖解決這一問題,但失敗了(從鏈接的回答以上):
InputStream audioSrc = getClass().getResourceAsStream("2MB_sound.wav");
InputStream bufferedIn = new BufferedInputStream(audioSrc);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);
我試圖並稱正是因爲以上(有關進口)的代碼,但它給了我一個不同的錯誤說:「不能從類型Object中對非靜態方法getClass()進行靜態引用」。所以現在我卡住了,回到我原來的代碼,如下所示。
請幫我解決我的問題。
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();
}
}
}
它也發佈在這裏:https://論壇。 oracle.com/forums/thread.jspa?threadID=2289395&tstart=0 – 2012-02-17 07:04:43