【发布时间】:2012-03-08 12:32:08
【问题描述】:
所以我遇到了与java.io.IOException: mark/reset not supported 相同的问题。
我希望它如何工作:
- 让程序打开一个弹出按钮,说“点我玩”
- 一旦用光标单击,将永远播放 2MB_sound.wav(是的,它的大小为 2MB)
问题是什么:
不知何故,我编写的名为 backgroundPlayer 的代码在我的 uni comps 的一个桌面上完全可以正常工作,但在我的笔记本电脑上却不行。在我的笔记本电脑上运行代码时,弹出按钮有效,但是当我单击它时......它给出了错误“java.io.IOException:不支持标记/重置”。
我为解决问题所做的尝试但失败了(来自上面的答案):
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();
}
}
}
【问题讨论】:
标签: java wav ioexception javasound