Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Background music
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hendo




PostPosted: Mon May 26, 2008 7:02 pm   Post subject: Re: Background music

code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
import javax.sound.sampled.*;

public class titleScreen extends Frame implements KeyListener
{
    private Image title;
    private Image cursor;
    private MediaTracker tracker;
    private Thread thread;
    private Image offScrImg;
    private int cursorX = 217;
    private int cursorY = 183;
    private int select = 1;


    titleScreen ()
    {
        addKeyListener (this);
        tracker = new MediaTracker (this);
        title = Toolkit.getDefaultToolkit ().getImage ("graphics/titlescreen.png");
        tracker.addImage (title, 0);
        cursor = Toolkit.getDefaultToolkit ().getImage ("graphics/Cursor.png");
        tracker.addImage (cursor, 0);
        try
        {
            tracker.waitForID (0);
        }
        catch (InterruptedException e)
        {
            System.out.println (e);
        }
        int width = title.getWidth (this);
        int height = title.getHeight (this);
        setSize (width, height);
        setVisible (true);

        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);
                }
            }

        }
        ;
        Thread thread = new Thread (background_music);
        thread.setDaemon (true);
        thread.start ();


    }


    public static void delay (int ms)
    {
        try
        {
            Thread.sleep (ms);
        }
        catch (Exception e)
        {
            ;
        }
    }
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 18 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: