Computer Science Canada Background music |
| Author: | Hendo [ Fri May 23, 2008 2:12 pm ] | ||
| Post subject: | Background music | ||
Ok, for my title screen, I need some background music to play and I thought this would do it for me.
however no sound plays and it makes me very sad face |
|||
| Author: | rizzix [ Fri May 23, 2008 3:36 pm ] | ||||
| Post subject: | RE:Background music | ||||
First of all, if you're playing a large music file, do not use clips! That would consume a LOT of memory. Secondly. You need to run the music in the background concurrently. Your current method will make your application unresponsive. Looking up the javaalmanac, here the method with streams:
However, you need to encapsulate all that inside a Runnable and run it in a Thread. Like this:
|
|||||
| Author: | Hendo [ 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 |
|
| Author: | Hendo [ 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? |
|
| Author: | rizzix [ 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. |
|
| Author: | Hendo [ 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 |
|
| Author: | rizzix [ 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. |
|
| Author: | Hendo [ Sun May 25, 2008 6:32 pm ] |
| Post subject: | RE:Background music |
oh! ok, thanks so much |
|
| Author: | Hendo [ 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 |
|
| Author: | rizzix [ Mon May 26, 2008 3:02 pm ] |
| Post subject: | RE:Background music |
Yea if it's that tiny, it's safe to use clips. |
|
| Author: | Hendo [ 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 |
|
| Author: | rizzix [ Mon May 26, 2008 3:50 pm ] |
| Post subject: | RE:Background music |
Yep. |
|
| Author: | Hendo [ Mon May 26, 2008 4:32 pm ] |
| Post subject: | RE:Background music |
sweet deal, thanks so much |
|
| Author: | Hendo [ 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:
|
|||
| Author: | rizzix [ Mon May 26, 2008 6:42 pm ] |
| Post subject: | RE:Background music |
Can you post the relevant code in its entirety? |
|
| Author: | Hendo [ Mon May 26, 2008 7:02 pm ] | ||
| Post subject: | Re: Background music | ||
|
|||
| Author: | rizzix [ Mon May 26, 2008 8:53 pm ] | ||||||
| Post subject: | RE:Background music | ||||||
The problem is with the following snippet
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:
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:
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. |
|||||||
| Author: | Hendo [ 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. |
|