2012-03-11 4 views
3

내 코드는 괜찮아요. 내 .jar 파일은 그 안에 .wav가 있습니다. 하지만 getResourceAsStream을 사용하여로드하려고하면 오류가 발생합니다.리소스에서 스트림을 읽는 Java 예외

java.io.IOException: mark/reset not supported 
    at java.util.zip.InflaterInputStream.reset(Unknown Source) 
    at java.io.FilterInputStream.reset(Unknown Source) 
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno 
wn Source) 
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) 
    at operation.MainWindowOperations.prepareAudio(MainWindowOperations.java 
:92) 
    at operation.MainWindowOperations.<init>(MainWindowOperations.java:81) 
    at graphics.LaunchGraphics.<init>(LaunchGraphics.java:25) 
    at run.RunApp.main(RunApp.java:14) 

이 내 코드입니다 :

private void prepareAudio() { 
    try { 

     InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav"); 
     inputStream = AudioSystem.getAudioInputStream(is); 
     clip = AudioSystem.getClip(); 
     clip.open(inputStream); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 

    } 

} 

누군가가 나를 도울 수

이 내 오류가? 많은 감사드립니다!!

+1

이 [post] [1]에서 답을 찾을 수 있습니다. [1] : http://stackoverflow.com/questions/5529754/java-io-ioexception-mark-reset-not-supported –

+0

@EricR. +1 그 스레드는 내가 모르고 있었던 두 가지 잠재적 인 지름길을 포함하고 있습니다. –

+0

나를 위해 일하고, 그것은 나에게 또 다른 오류를 제공하지 그 ... 스트림이 닫혀 .. – TiagoM

답변

6

Java Sound에서는 일부 조작을 위해 위치 조정 가능한 (마크/재설정 지원) 입력 스트림이 필요합니다. 이 문제를 해결하면 스트림의 위치를 ​​재조정 할 수 없기 때문입니다.

원래 스트림의 byte[]을 표시/재설정을 지원하는 ByteArrayInputStream에 넣는 것이 한 가지 방법입니다.


에릭 연구에서 링크 된 질문에 대한 두 번째 대답도 가능성이 있으며 더 간단 해 보입니다. ... 변경을 시도

InputStream is = this.getClass().getClassLoader().getResourceAsStream("beep.wav"); 
inputStream = AudioSystem.getAudioInputStream(is); 

으로하려면

URL url = this.getClass().getClassLoader().getResource("beep.wav"); 
inputStream = AudioSystem.getAudioInputStream(url); 
+0

정말요? 자바는 소리가 나쁘다. 바이트 배열을 사용해야합니까? 그런 다음 전체 배열을 각각의 바이트로 재생할 수 있습니까? : 미리 감사드립니다. – TiagoM

+0

* "전체 배열을 루프하여 각 바이트를 반복합니다."* 아니요,'ByteArrayInputStream'을'AudioSystem.getAudioInputStream (..)'에 전달하면 나머지 코드는 그대로 작동 할 수 있습니다. –

+0

나는 Andrew가 무슨 뜻인지 이해하지 못했다. 의미 : ByteArrayInputStream b = (ByteArrayInputStream) this.getClass(). getResourceAsStream ("beep.wav"); \t \t \t \t inputStream = AudioSystem.getAudioInputStream (b); ? 미리 감사드립니다. – TiagoM

1

내가 JavaRanch에서 팀 무어의 도움으로 대답을 얻었다. 나는 앤드류가 대답의 두 번째 부분에서이 정답을 언급 했음에도 불구하고 가장 명확한 것은 여기에 게시하는 것이 될 것이라고 생각했다. (첫 번째 부분은 너무 작동하지만 과잉이다.)

Url url = this.getClass().getResource("beep.wav");   
inputStream = AudioSystem.getAudioInputStream(url); 

http://www.coderanch.com/t/558274/Applets/java/mark-reset-not-supported-getResourceAsStream

Tim wrote: I have no specific knowledge about this problem, or audio in applets in general, but I'm not surprised that mark/reset doesn't work with resources obtained through the ClassLoader mechanism. (Actually, I'm sort of surprised it works at all, at least some of the time :-)

Assuming that the audio file is publicly accessible through HTTP, try AudioSystem.getAudioInputStream(URL) instead of the InputStream version you're using now. Looking at the javax.sound.sampled.spi.AudioFileReader javadocs (which is the class being used underneath), only the InputStream variant talks about mark/reset problems, not the URL version.

이 또한 여기 와서 : https://forums.oracle.com/forums/thread.jspa?threadID=2289395&tstart=0 과 대답은 오라클 버그 참조와 함께 바닥 근처 # 7095006 그것은 재미있는 읽기입니다 (원래 op 시도한) 코드가 작동하지만 더 이상 않습니다 왜 설명합니다.

+0

감사합니다. 문제가 해결되었습니다. – TiagoM

+0

+1 좋은 답변입니다. 해피 1K. :) –

+0

Thx Andrew! 당신이 내 첫 +10 점 이었음을 기억합니다. 새해 복 많이 받으세요! –

관련 문제