Hey everyone,I'm new to this website and also kinda new to Java but am using BlueJ to code a game as a side project. I am making a 2d Fighting Game. I don't know how to make it so when it punches it makes the sound, so like A and D to walk left and right across the screen and E for punch and R to kick, I'm trying to make it so when you press the corresponding buttons the sounds will play for either punching or kicking. Can anyone help me with this? Thanks In Advance

Sound doesn't have to be complicated, either. The easiest option is probably my SoundSystem Library (perhaps overkill for your project, but very easy to use). You would need the SoundSystem core, LibraryJavaSound plug-in, and a codec plug-in (CodecWav, for example). Assuming you have sound files named "punch.wav" and "kick.wav", you would compile them into your JAR in a package/directory named "Sounds". Here is Demonpants' example with sound effects added in:

// Add these imports at top of the program:importpaulscode.sound.SoundSystem;importpaulscode.sound.SoundSystemConfig;importpaulscode.sound.SoundSystemException;importpaulscode.sound.libraries.LibraryJavaSound;importpaulscode.sound.codecs.CodecWav;//...etc

That should work fine for playing short clips. Background music depends on the length of the song and the format.

There is a maximum size that an audio clip can be (generally around 5 seconds or so, depending on the quality). If your song is longer than that, you will need to "stream" it. That's where you feed the data to a line in chunks over time (as one chunk finishes, you feed in another). Most developers will run that process on a separate Thread.

If your song is a MIDI file, you don't stream it. MIDI is a complicated format, not like other audio formats. In that case, you would read the data from the MIDI file (called the Sequence), pass that information to an available Sequencer, and link that to an available Synthesizer which outputs the music.

If you need to do either of these two options (i.e. if your music is too long or if it is MIDI), then I recommend using the SoundSystem library I mentioned earlier, because it makes both of these processes much easier (1 line of code vs. 30 lines).After initializing the SoundSystem as mentioned in my earlier post, you can simply use the backgroundMusic method (works for both MIDI and for long audio files):

If you want to use AudioClip to play music (AudioClip, in my opinion, is the easiest way to get sound to work at a very minimal level), you can just link a sounds like you did otherwise except call mySound.loop() instead of mySound.play(). The big problem with this, aside from AudioClip sucking many ways in general, is that you can't use compressed audio in any fashion whatsoever. So your game will be 99% sounds (in terms of disk space) if you don't use compressed audio.

paulscode's library is great, but it might be too difficult for you at this point to use external libraries - if you're not worried about file size and potential issues just stick with AudioClip.

I just noticed you were using java.applet.AudioClip (for some reason I interpreted this as javax.sound.sampled.clip). Disregard my comment about the length limitation. AudioClip can play longer files I believe, and it supports the wav, .au, and .aif formats. The limitation with this method is that it only provides play(), loop(), and stop() functions, and the supported file formats can get rather large in size as Demonpants pointed out (especially if you are considering music). Otherwise, it is a simple solution.

We love death. The US loves life. That is the difference between us. -Osama bin Laden, mass murderer

Also if I wanted a sound to play right at the beginning to say like Fight! or something like that how would I just add that to play once at the very beginning and then not play anymore till the next time it is played, or make it so at the end of the game it says You Win! Thanks for all the help

I've never used AudioClip myself, to be honest. Is there a limitation with this class preventing you from creating and playing a couple of AudioClip's for your "Fight!" sound and the background music, somewhere at the beginning of your program (say in the init() method)?

vection, you should probably study up a bit more on Java in general if you can. Your questions are not really sound related. Here is a simple pseudo code game that has background music and plays sounds when you press buttons.

That should hopefully illuminate things for you, even though the code's design is totally retarded (for example, you wouldn't play sounds from the main class, you would have player.punch() play the punch sound, and world.startGame() play the fight sound and the music) and the game loop is totally BS.

I've never used AudioClip myself, to be honest. Is there a limitation with this class preventing you from creating and playing a couple of AudioClip's for your "Fight!" sound and the background music, somewhere at the beginning of your program (say in the init() method)?

I wouldn't recommend doing it exactly as paulscode suggested, as you're instantiating a new sound every single time you play it, which is crazy wasteful and really really slow. I think he put that example there just to give you a clearer idea of what's going on. Instead, do as I suggested, and store the sound somewhere, only to play it later.

And honestly I'm sort of frustrated with you at this point as you don't seem to be actually reading what we're saying to you. You have now been told three times how to loop a sound.

Quote

If you want to use AudioClip to play music (AudioClip, in my opinion, is the easiest way to get sound to work at a very minimal level), you can just link a sounds like you did otherwise except call mySound.loop() instead of mySound.play().

Quote

music.loop();fightSound.play();

Quote

As Demonpants suggested, why not just use loop() instead of play()?

Similarly, you asking now how to make an ouch sound shows that in general you just don't understand what you're doing yet. Go back to basics and learn how code / Java work, because your comprehension right now is severely limited. To play a sound, anywhere, at any time, you use sound.play() at the bit of code where you would want the sound to play. It's the same concept. Exactly the same.

I wouldn't recommend doing it exactly as paulscode suggested, as you're instantiating a new sound every single time you play it, which is crazy wasteful and really really slow.

Just to clarify, my suggestion was for the two sounds that are played only once, in which case you would not be instantiating a new sound every single time you play it, because you are only playing it once.

We love death. The US loves life. That is the difference between us. -Osama bin Laden, mass murderer

Just to clarify, my suggestion was for the two sounds that are played only once, in which case you would not be instantiating a new sound every single time you play it, because you are only playing it once.

Sound doesn't have to be complicated, either. The easiest option is probably my SoundSystem Library (perhaps overkill for your project, but very easy to use). You would need the SoundSystem core, LibraryJavaSound plug-in, and a codec plug-in (CodecWav, for example). Assuming you have sound files named "punch.wav" and "kick.wav", you would compile them into your JAR in a package/directory named "Sounds". Here is Demonpants' example with sound effects added in:

// Add these imports at top of the program:importpaulscode.sound.SoundSystem;importpaulscode.sound.SoundSystemConfig;importpaulscode.sound.SoundSystemException;importpaulscode.sound.libraries.LibraryJavaSound;importpaulscode.sound.codecs.CodecWav;//...etc

SoundSystem.jarLibraryJavaSound.jarCodecWav.jar (or any other codecs that you will be using)

Step #3 Tell the Java compiler about the .jar files from step 2. How to do this step depends entirely on which GUI you are using to develop your project (if you are using one), so I can't explain that here (google is your friend). I personally use NetBeans, so if that is what you are using, I can explain how to do this in more detail.

Step #4 Place all the audio files you will be using into a package named "Sounds/". Again, this depends on your GUI.

Step #5 Include the necessary imports at the top of your program. For example:

Step #11 Play the sound effects when it is time to play them. For example:

1

quickPlay( "fight.wav" );

Thats about it. The SoundSystem library is quite simple as audio libraries go, so you shouldn't have too much trouble using it once you are comfortable programming in Java.

I recommend reading the tutorial I wrote 3D Sound with SoundSystem if you are serious about using the library, as it will familiarize you with the termonology and walk you through several example programs.

We love death. The US loves life. That is the difference between us. -Osama bin Laden, mass murderer

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org