【问题标题】:java.io.IOException: mark/reset not supported (for static)java.io.IOException:不支持标记/重置(对于静态)
【发布时间】: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


    【解决方案1】:

    我不得不处理一个非常相似的问题,并在这里发布:

    mark/reset exception during getAudioInputStream()

    这种形式:.getResourceAsStream(fileName) 返回一个 InputStream,如果文件不可标记,则抛出标记/重置异常。我得到的解释是 .wav 曾经有一个默认的“第一次猜测”,但这不再是第一次猜测(从 Java 7 开始)。在 Oracle 的错误数据库中对 #7095006 有更好、更完整的描述。

    使用这个表单应该没问题,因为它不需要支持标记和重置的中间步骤(InputStream):

    URL url = AudioMixer.class.getResource(fileName); 
    AudioInputStream ais =  AudioSystem.getAudioInputStream(url);  
    

    【讨论】:

    【解决方案2】:

    在链接的问题中,底层基础数据流的构造略有不同,因此您必须调整解决方案。

    而不是这个:

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

    使用这个:

    InputStream audioSrc = new FileInputStream("85046_newgrounds_parago.wav");
    

    【讨论】:

    • 谢谢你回答 AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);但是现在这段代码有一个问题,同样的说法是“java.io.IOException:不支持标记/重置”
    【解决方案3】:

    此代码编译。

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

    【讨论】:

    • 我尝试编译你的代码,但它给了我这个错误“java.io.IOException: Stream closed”
    • 我通常这样做的方式是加载声音的整个byte[] 并将其放入ByteArrayIntputStream。您可以确定 BAIS 是可重新定位的。
    • 我也得到了这个“java.io.IOException: Stream closed”
    猜你喜欢
    • 2015-06-28
    • 2014-01-02
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多