This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Playing sound from a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Playing sound from a jar file" Watch "Playing sound from a jar file" New topic
Author

Playing sound from a jar file

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I am having problems trying to access sound resources from my jar file. They reside in the directory "/sounds/" in the jar. I am not able to point to the sound file from my code using a URL.



When i run the code outside of the jar everything is fine. But when i put everything into a jar the URL class is unable to reach the wav file. I have the jar in my classpath. A work around could be to use

AudioClip clip = Applet.newAudioClip(url);
clip.play();

But thats not a solution here because once i switch to clip.play() instead of using audio input stream the audio does not play sometimes when called in quick successions or it waits a long time ( around 2 seconds ) before it plays the clip. Does any one know a way through which i can still access the wav files using an audio input stream ? Or can you suggest a way through which i can improve the performance of clip.play() ?

Any suggestions are welcome.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Hey i figured it out ! I am able to stream sound files with the following code.



Just posting to let people know about this solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Playing sound from a jar file
 
Similar Threads
Audio format conversion problems
Get wav file reference from resource map
Play a .wav file
Saving sound as byte array
Getting audio to play in the background of my GUI.