2012-02-17 71 views
1

所以我遇到类似于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(); 
    } 
} 

}

回答

2

我不得不面对一个非常类似的问题,在这里贴吧:

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); 
+0

它也发布在这里:https://论坛。 oracle.com/forums/thread.jspa?threadID=2289395&tstart=0 – 2012-02-17 07:04:43

1

在链接的问题,底层基本数据流构造略有不同,所以你必须适应该溶液中。

取而代之的是:

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

使用此:

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

THX为你解答 的AudioInputStream语音串流= AudioSystem。 getAudioInputStream(bufferedIn); 但是现在这段代码有一个问题,用同样的说法“java.io.IOException:标记/重置不支持“ – compski 2012-02-17 04:30:03

0

此代码编译。

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

我试着编译你的代码,但它给了我这个错误”java.io.IOException:流关闭“ – compski 2012-02-17 04:58:29

+0

我通常这样做的方式是加载整个'byte []'声音并将其放在一个'ByteArrayIntputStream'中,您可以确定BAIS是可重定位的。 – 2012-02-17 05:31:04

+2

我也得到这个“java.io.IOException:流关闭” – 2012-08-02 13:34:56

相关问题