2012-02-17 75 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

相關問題