// From URL
// stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat ();
if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
{
format = new AudioFormat (
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate (),
format.getSampleSizeInBits () * 2,
format.getChannels (),
format.getFrameSize () * 2,
format.getFrameRate (),
true); // big endian
stream = AudioSystem.getAudioInputStream (format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info (
Clip.class, stream.getFormat (), ((int) stream.getFrameLength () * format.getFrameSize ()));
Clip clip = (Clip) AudioSystem.getLine (info);
// This method does not return until the audio file is completely loaded
clip.open (stream);
// From URL //stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted // to PCM_SIGNED before it can be played AudioFormat format = stream.getFormat();
if(format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED){
format = newAudioFormat( AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// From URL //stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted // to PCM_SIGNED before it can be played AudioFormat format = stream.getFormat();
if(format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED){
format = newAudioFormat( AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
Posted: Sat May 24, 2008 10:15 am Post subject: Re: Background music
thanks for the thorough response, i'll test it out, thanks so much
Hendo
Posted: Sat May 24, 2008 10:41 am Post subject: Re: Background music
Hm, it doesn't like the Override commad
it says illegal token when I try to run
If I'm correct I delete my existing run method and replace it with yours? and then call the thread (new Thread (background_music).start (); ) in the titlescreen() section?
rizzix
Posted: Sat May 24, 2008 4:32 pm Post subject: RE:Background music
Delete the @Override annotation (you're using and older version of Java).
what you do here is different. You first remove you "implements Runnable". Delete your run method. And pretty much copy-paste the 2nd half into your constructor.
You might have to change the code a bit. For example you most likely want to make the Thread a daemon thread.
Hendo
Posted: Sun May 25, 2008 9:56 am Post subject: RE:Background music
daemon thread? :S
I'm really new to JAVA so I'm not sure what you mean by that
Thanks for all your help so far though
rizzix
Posted: Sun May 25, 2008 1:35 pm Post subject: RE:Background music
Yea, using thread.setDaemon(true).
This will ensure that your program will quit when you quit your application. If you don't make the tread a daemon, then your program will continue to run till the music ends.
Hendo
Posted: Sun May 25, 2008 6:32 pm Post subject: RE:Background music
oh! ok, thanks so much
Sponsor Sponsor
Hendo
Posted: Mon May 26, 2008 12:38 pm Post subject: RE:Background music
Ok, I got it to work, but is there any way to loop DataLines? Or should I go back to trying to use clips? The sound is 13 seconds long and I just need to loop it
rizzix
Posted: Mon May 26, 2008 3:02 pm Post subject: RE:Background music
Yea if it's that tiny, it's safe to use clips.
Hendo
Posted: Mon May 26, 2008 3:26 pm Post subject: Re: Background music
alrighty cool, will the code i originally posted work fine (except modified so it loops), as long as it uses the runnable and threads that you used?
p.s. rizzix your a god send
rizzix
Posted: Mon May 26, 2008 3:50 pm Post subject: RE:Background music
Yep.
Hendo
Posted: Mon May 26, 2008 4:32 pm Post subject: RE:Background music
sweet deal, thanks so much
Hendo
Posted: Mon May 26, 2008 5:09 pm Post subject: RE:Background music
Ok, for some reason my graphics don't show up until the song is over...is there something wrong with my threads? They look like this:
code:
Thread thread = new Thread (background_music);
thread.setDaemon (true);
thread.start ();
rizzix
Posted: Mon May 26, 2008 6:42 pm Post subject: RE:Background music