Songs within Applications

Will someone please give me the basic code needed to play a song within an application. Instead of giving me a link to a website could you actually paste the necessary code into the forum please. I also have a question on what types of audio files can be played. I have been trying to play a .wma file. Is that compatible? Please help.

[342 byte] By [Ajd123a] at [2007-10-3 5:14:42]
# 1
You can use AudioClips to play short sound files. For long ones (3+ minutes), check out [url= http://forum.java.sun.com/thread.jspa?forumID=513&threadID=450768]this[/url] thread.
CaptainMorgan08a at 2007-7-14 23:21:15 > top of Java-index,Security,Cryptography...
# 2
Is all of that code really necessary to just play one song when say a button is clicked?
Ajd123a at 2007-7-14 23:21:15 > top of Java-index,Security,Cryptography...
# 3

it seems so... I've been trying to find a simple way to play a sound for quite some time, and the only thing that seemed ok was a trick with using some undocumented features (sun.audio.* package)...

here it is, but mind you, it's not something that should be normally used:

import sun.audio.*;//import the sun.audio package

import java.io.*;

//** add this into your application code as appropriate

// Open an input stream to the audio file.

InputStream in = new FileInputStream(Filename);

// Create an AudioStream object from the input stream.

AudioStream as = new AudioStream(in);

// Use the static class member "player" from class AudioPlayer to play

// clip.

AudioPlayer.player.start(as);

// Similarly, to stop the audio.

AudioPlayer.player.stop(as);

To use a URL as the audio stream source, substitute the following for the input stream and audio stream setup:

AudioStream as = new AudioStream (url.openStream());

Playing the audio stream continuously adds a bit more complexity:

// Create audio stream as discussed previously.

// Create AudioData source.

AudioData data = as.getData();

// Create ContinuousAudioDataStream.

ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);

// Play audio.

AudioPlayer.player.play (cas);

// Similarly, to stop the audio.

AudioPlayer.player.stop (cas);

And there you have it. Remember, this technique uses undocumented features; there are no guarantees that it will work with anything but the current Sun JDK.

phantasmoa at 2007-7-14 23:21:15 > top of Java-index,Security,Cryptography...
# 4
I put in that "shortcut code" in my application and it worked! All I had to do was convert my wma file to a wav. Thanks so much!Message was edited by: Ajd123
Ajd123a at 2007-7-14 23:21:15 > top of Java-index,Security,Cryptography...
# 5
Ooops, I forgot to mention that wma is not supported... Well, you found that one out on your own :)Anyway, glad you made it work!
phantasmoa at 2007-7-14 23:21:15 > top of Java-index,Security,Cryptography...