Advertisement Jump to content
Sign in to follow this  

Sound with JMF in java

This topic is 4231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, my fellow java noobs and noobettes. I was just wondering if any of you had a good simple example of how to use JMF to play sounds/music in your game? I had a look at the applet example on the page above designed for webpages but it was like Hebraic/Ancient Greek/Moonspeak to me. Currently I'm using AudioClip together with the simple class below to create sounds in my game. For an example by calling soundCache.loopSound("battletheme.mid"); Sound Cache class: ----------------------------------------------- import java.applet.Applet; import java.applet.AudioClip; import; import java.util.HashMap; public class SoundCache{ protected HashMap resources; public SoundCache() { resources = new HashMap(); } protected Object loadSound(String name) { URL url=null; url = getClass().getClassLoader().getResource(name); return loadSound(url); } protected Object getSound(String name) { Object res = resources.get(name); if (res == null) { res = loadSound("sounds/" + name); resources.put(name,res); } return res; } protected Object loadSound(URL url) { return Applet.newAudioClip(url); } public AudioClip getAudioClip(String name) { return (AudioClip)getSound(name); } public void playSound(final String name) { new Thread( new Runnable() { public void run() { getAudioClip(name).play(); } } ).start(); } public void loopSound(final String name) { new Thread( new Runnable() { public void run() { getAudioClip(name).loop(); } } ).start(); } } ----------------------------------------------- Feel free to use it if you want. But the problem with this solution is that the midi files only work sometimes when you play them. For an example it works sometimes when i have media player playing some other audio file while playing the game. I've tried this on 5 computers. 1 worked perfectly always, 4 had sound but it had extra noise that made it sound bad, and there was no sound on the last whatsoever.... Wave files always worked. I hope you can explain the problem to me on a level I can understand. I'm a total java-sound noob.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!