How to play a sound whose resource is within a JAR ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Daniel_Rosenberg
Posted On:   Monday, July 9, 2001 09:07 AM

Hello, I try to play a sound whose resource is in a JAR but doesn't manage to do it. Here is the core code: JarFile jar = new JarFile(name); Enumeration enum = jar.entries(); while(enum.hasMoreElements()) { ZipEntry entry = (ZipEntry) enum.nextElement(); if ( entry.getName().indexOf("sound") != -1 && !entry.isDirectory() && isSoundFile(entry.getName())) { int beginIndex = entry.getName().lastIndexOf('/'); String entryName = (beginIndex!=-1)?entry.getName().substring(beginIndex+1):entry.getName(); InputStream is = jar.getInputStream(entry); System.out.println("i   More>>

Hello,


I try to play a sound whose resource is in a JAR but doesn't manage to do it.



Here is the core code:


JarFile jar = new JarFile(name);


Enumeration enum = jar.entries();

while(enum.hasMoreElements()) {

ZipEntry entry = (ZipEntry) enum.nextElement();

if ( entry.getName().indexOf("sound") != -1 && !entry.isDirectory() && isSoundFile(entry.getName())) {

int beginIndex = entry.getName().lastIndexOf('/');

String entryName = (beginIndex!=-1)?entry.getName().substring(beginIndex+1):entry.getName();

InputStream is = jar.getInputStream(entry);

System.out.println("is.markSupported()"+is.markSupported());

AudioInputStream ais = new AudioInputStream(is, AudioSystem.getAudioFileFormat(is).getFormat(), 0);

addSound( name, ais);

}

}


public boolean isSoundFile(String s){

return (s!=null)?
(s.endsWith(".au") || s.endsWith(".rmf") ||
s.endsWith(".mid") || s.endsWith(".wav") ||
s.endsWith(".aif") || s.endsWith(".aiff")):false;

}



When I run it, the input stream is not null but I get the exception:


java.io.IOException: mark/reset not supported?


So I cannot create the AudioInputStream.


Any idea would be welcome.


Thanks

Daniel

   <<Less

Re: How to play a sound whose resource is within a JAR ?

Posted By:   Christopher_Schultz  
Posted On:   Friday, August 10, 2001 07:57 AM

Instead of opening up the JAR file yourself, have the classloader do it:




InputStream is = this.getClass().getClassLoader().getResourceAsStream("audio.wav");

AudioInputStream ais=newAudioInputStream(is);



Then, you don't have to worry about the ZIP stuff.



-chris
About | Sitemap | Contact