PDA

View Full Version : How can I play music files in a java program?



ThePheonix
11-12-2005, 06:48 PM
How can I play music files in a java (NOT JavaScript) program?

Preferably mp3's as I know how to play mid files (don't come out too well though)

Leeza
11-12-2005, 06:50 PM
*moves to Help*

bipper
11-12-2005, 08:44 PM
While I am not just going to give you an answer, I can give you a resource to read. Point is, there are a billion different ways to do this, but the best would be to learn and understand the JMF: Java medi frame work.

It can be a bare if you are not to experienced with Java, but it is a clean useful set to use - even though it is java ;)
Here is the basic FAQ: http://java.sun.com/products/java-media/jmf/2.1.1/faq-jmf.html

This is a dated article, but it I still have it in my Bookmarks ;) it is an interesting article, and an ok read. Not much educationl value, but more of a preception on the purpose of the JMF.
http://www.javaworld.com/javaworld/jw-06-1999/jw-06-media.html

This again, is just one way to aim what you are going for. Like anything else in Java, it is clunky, and a tad slow. The set makes life a ton easier when working with media though :)
good luck ;)

Bipper

o_O
11-13-2005, 01:02 AM
MP3 is a bit of a format to avoid when it comes to Java, since it isn't natively supported. If you could sacrifice the MP3s for MIDI or WAV files, a quick and easy method you could include would be something like:


public AudioClip loadSound(String filename) {
URL soundPath = getClass().getResource(filename);
return Applet.newAudioClip(soundPath);
}


To access this method, just initialise a variable of type AudioClip, and pass the method the name or path of the string:


AudioClip sound = loadSound("hello.wav");


To start or stop the sound from playing:


sound.play();
sound.stop();


Also, note that to use this method, you need to import the java.net.* and java.applet.* packages.