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

Username:   Password: 
 RegisterRegister   
 Java Jeopardy Game Help with Sound
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AshleyI




PostPosted: Fri Jan 19, 2007 9:11 pm   Post subject: Java Jeopardy Game Help with Sound

Hi,

Can someone kindly help me and show me how I can play an audio clip at the beginning of my game. I already have the audio clip and it's a URL. I only have basic knowledge in Java. I tried searching and this is what I found so far:

code:
try {
        // From file
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
   
        // 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);
   
        // Start playing
        clip.start();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (LineUnavailableException e) {
    } catch (UnsupportedAudioFileException e) {
    }


I pasted this code in a seperate method; and changed the URL link and I get 24 errors Shocked Sad ... that a lot of these variables are not defined such as stream, AudioFormat etc.. Should I be declaring something in class or importing?? Please help and oh I m using Visual Age for Java 2.0.

P.S. Yes, This topic had been posted before and there is the same link given to the code.. but no explanation. And I absolutely have no idea what to do next..

Thanks,
Ashley.

[/url]

Edit: wtd added code tags
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Sat Jan 20, 2007 5:36 pm   Post subject: Re: Java Jeopardy Game Help with Sound

AshleyI wrote:
I pasted this code in a seperate method; and changed the URL link and I get 24 errors Shocked Sad ... that a lot of these variables are not defined such as stream, AudioFormat etc.. Should I be declaring something in class or importing??

All variables must be declared, or else they cannot be used. As for AudioFormat, it's a class, and that you must import from the javax.sound.sampled package. Unfortunately, I don't understand too much about the javax.sound.sampled package, but I do remember that it was really confusing and complicated, and I ended up using the Applet class's AudioClip interface for my own project.

If your program is an applet, you can use your applet's built-in method getAudioClip (url), or use Applet's static method, Applet.newAudioClip (url), to create a new AudioClip object, and you don't have to worry about formats or anything else like that.
AshleyI




PostPosted: Sat Jan 20, 2007 7:13 pm   Post subject: RE:Java Jeopardy Game Help with Sound

Unfortunately, my game is created in consoles. And I tried to import javax.sound.sampled.*; and I am getting an error: Import refers to a missing package. Does anyone know anything about that? And how do I declare other variables? The Q may sound stupid, but I have no idea what I m doing anymore. Shocked


Ashley.
HellblazerX




PostPosted: Sun Jan 21, 2007 12:29 pm   Post subject: Re: RE:Java Jeopardy Game Help with Sound

AshleyI wrote:
Unfortunately, my game is created in consoles.

Hmmmm, that certainly is a problem. I don't know much about consoles, mainly cuz I don't use them, but I do know the RTP's console uses its own separate library, and you'd have to find the directory where the library is located, and add in the packages in manually. I would recommend, however, that you use the Applet package. I'm pretty sure your console should have it.

AshelyI wrote:
And how do I declare other variables

You declare variables in a line by first stating the type of object they'll be referring to, and the name of the variable:
Java:
AudioInputStream stream

AudioInputStream would be the type of object that the variable is referring to, and stream is the name of the variable.
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 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: