java - Path to read a sound out of my package -


i have sons class load , play sounds. , adhd class contain main , uses sons class.

all classes in package "adhd" , sounds in jar, : 1.wav in soundn in jar. (adhd.jar/soundn/1.wav).

when run code in eclipse works, when run jar doesn't. important me keep sounds "loading" because need program read sounds quickly, using timers. suggest me do?

here code of class sons load sounds in instances of singletons.

sons

package adhd;  import java.io.file;  import java.io.ioexception;  import javax.sound.sampled.audioformat;  import javax.sound.sampled.audioinputstream;  import javax.sound.sampled.audiosystem;  import javax.sound.sampled.dataline;  import javax.sound.sampled.floatcontrol;  import javax.sound.sampled.lineunavailableexception;  import javax.sound.sampled.sourcedataline;  import javax.sound.sampled.unsupportedaudiofileexception; import java.applet.applet; import java.applet.audioclip; import java.net.url;  public class sons {     private static string path=null;     private static sons instance;     private final map<string, clip> sons;     private boolean desactive;      sons(string path) {         path = path;         sons = new hashmap<string, clip>();     }       public void load(string nom) throws unsupportedaudiofileexception, ioexception, lineunavailableexception {         clip clip = audiosystem.getclip();         clip.open(audiosystem.getaudioinputstream(getclass().getresourceasstream(path + nom)));         sons.put(nom, clip);     }       public void play(string son) {         if(!desactive) try {             clip c = getson(son);             c.setframeposition(0);             c.start();         } catch(exception e) {             system.err.println("impossible play sound " + sound);             desactive = true;         }     } } 

here adhd class contain main uses sounds

main class : adhd

public static void main(string[] args) {      sons sonn= new sons("/soundn/");      try {         sonn.load("1.wav");     } catch (unsupportedaudiofileexception | ioexception             | lineunavailableexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     sonn.play("1.wav"); } 

here picture of tree

tree

now, exception message, know problem is. problem not sounds can't loaded or aren't found in jar. problem that, the javadoc says:

these parsers must able mark stream, read enough data determine whether support stream, and, if not, reset stream's read pointer original position. if input stream not support these operation, method may fail ioexception.

and stream returned class.getresourceasstream(), when resource loaded jar, doesn't support these operations. read input stream byte array in memory, create bytearrayinputstream byte array, , pass stream audiosystem.getaudioinputstream().

if loading in memory not option because sound long (but guess wouldn't put in jar), write temporary file, , pass fileinputstream audiosystem.getaudioinputstream().


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -