[java] Swinging sound

Started by
7 comments, last by JediMaster 22 years, 11 months ago
Hi I''m working on a game in Java (it''s a University project). I started it out as an applet but now I''m moving to an independent program. I used AudioClip to play the sounds, but since it belongs to java.applet, I can''t use it now... does Swing has sound support capabilities? Or is there any other way out of it? For the bored ones, I''ve got a couple of screenshots up at http://student.dei.uc.pt/~pamaro/screens.htm (using graphics taken from SNES games to test out the engine ). JediMaster
Advertisement
Use the static Applet.getAudioClip method to load the clip and the use the clip''s play, loop and stop methods.

Cool looking game. Are you going to put it on the web when you''re done?
Hi
Will it work under a class extending JFrame? Since it''s an Applet method... (I haven''t yet tried it out, I''m converting everything to an independent program, should have the conversion done in a couple of hours).
As for releasing it, that''s the plan. It''s a project for my University and I want to have a really good grade at it, so I''ll definitely finish it . After that, I''ll probably release it on the net as freeware (assuming I can get someone to take care of the artwork). I have to deliver the project with a report till the 31st of May... so in early June I''ll probably have it on-line (maybe with a manual in HTML and stuff like that).

JediMaster
It''s static so you can call it anywhere without instantiating an applet. I assume you''re using JDK 1.2 or higher since you''re using swing. If you''re using 1.1 then it won''t work.

Just curious, are you using JScrollPane to do the scrolling?
I have created an application that plays sound fx.

import sun.audio.*;

Variables
byte play_snd_data[]; remember to fill it with something!
AudioData snd_audio;
AudioDataStream snd_stream;

to play
snd_audio = new AudioData(play_snd_data);
snd_stream = new AudioDataStream(snd_audio);
AudioPlayer.player.start(snd_stream);

to stop
AudioPlayer.player.stop(snd_stream);

and there is a method to play looping to

:-)

Hope that helps!



Hi

buh: hmmm... the problem is with getCodeBase(), since I need it to get the sound loaded and, unless I''m doing an applet, I can''t do it (or am I wrong...?)
As for the scrolling, I''m doing it myself. The game has 4 scrolling levels (two are parallax)... I chose to do it myself to be sure it would work out just the way I want .
As for te JDK, it''s the 1.3.something (latest one, I think)

Anonymous Poster: thanks, I''ll try that out

Thanks for the help, both of you . I''ll see if I can get it working (I''ve already moved it to an independent program... running even faster than before... I may be able to either improve the graphics or increase the frame''s size... I''ll have to decide about that)

JediMaster

the sun.audio.* classes don't exist in 1.3. There were some notes about how they might disappear, and sun also said that if code uses any sun.*.* classes then the code ain't 100% pure java.
Oh well.
On a more positive note if you do want sound to work no matter what jdk you have, try using a factory method. Basically
you ask the method for an AudioClip, it then works out what is the best way to give you that clip (sun.audio for pre 1.2 or Applet.newAudioClip for post 1.2). If you use some cunning introspection and reflection you can find out whether the required methods/classes actually exist or not.

e.g.

try {
Class appletClass = Class.forName( "java.applet.Applet" );
Class[] methodParams= { Class.forName( "java.net.URL" ) };
Method method = getDeclaredMethod(
"newAudioClip", methodParams );
}
catch( NoSuchMethodException nsme ) {
// if you get here the method does not exist
}


Edited by - lilspikey on April 27, 2001 6:10:42 AM
Yep, I noticed that thing about the sun.audio when I went to try it out... so I''m still stuck... the AudioClip seems to be the only way to do it... the only problem happens with the getCodeBase() part. Any ideas on how to get over this problem? It''d be boring to leave sound out of the game...

JediMaster
There are actually a couple javax.sound packages in JDK 1.3. The applet method is probably the easiest, though. If you need to identify a file in an URL you use new URL("file:buh.wav") or something like that. It might be in the java.net.URL documentation.

Edited by - buh on April 29, 2001 12:51:42 PM

This topic is closed to new replies.

Advertisement