Runnable background_music = new Runnable ()
{
public void run ()
{
try
{
AudioInputStream stream = AudioSystem.getAudioInputStream (new File ("ares.wav"));
// 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 line
SourceDataLine.Info info = new DataLine.Info (
SourceDataLine.class, stream.getFormat (),
((int) stream.getFrameLength () * format.getFrameSize ()));
SourceDataLine line = (SourceDataLine) AudioSystem.getLine (info);
line.open (stream.getFormat ());
line.start ();
// Continuously read and play chunks of audio
int numRead = 0;
byte[] buf = new byte [line.getBufferSize ()];
while ((numRead = stream.read (buf, 0, buf.length)) >= 0)
{
int offset = 0;
while (offset < numRead)
{
offset += line.write (buf, offset, numRead - offset);
}
}
line.drain ();
line.stop ();
}
catch (Exception e)
{
e.printStackTrace ();
System.exit (1);
}
while (true)
{
repaint ();
delay (10);
}
}
Posted: Mon May 26, 2008 8:53 pm Post subject: RE:Background music
The problem is with the following snippet
code:
while(true)
{
repaint ();
delay (10);
}
It blocks execution. In fact it does not let the "Frame" to be constructed, since the constructor never does return.
Now this is interesting, you've got me thinking. I wonder if it's wise to put that as part of the EDT. Hmm. My guess is, no, however you definitely need to call repaint inside the EDT. However I'm not too sure, again.
So, here's what I suggest, you create a Runnable to be executed in the EDT. Like this:
code:
Runnable repaint_in_edt = new Runnable() {
public void run() {
repaint ();
}
};
Now in your main-thread (this is the thread that contains the code which creates the titleScreen object, etc). After the creation of everthing, add a while-loop:
code:
/// initialization / instantiation
while (true) {
SwingUtilities.invokeAndWait(titleScreen_obejct.repaint_in_edt);
delay(10);
}
Here the repaint_in_edt is a field of the class titleScreen (Bad naming convention btw, it should be TitleScreen).
Or you can simply create another thread that just runs that loop inside your constructor.
Hendo
Posted: Tue May 27, 2008 6:42 am Post subject: RE:Background music
ok, this my sound weird but heres the thing. I'm at home right now, so i have one copy of my program here and another copy at school. The strange thing is my school one works fine without your code above...there must be a small dference in my school file compred to my file here at home. When I gt to school, I'll post the code to maybe explain how I pulled that miracle off, lol.