Background: I have some experience in programming, but it's not a lot and not very good. I'm taking my first Java class this year, but because the pace is so slow I decided to just learn with the aid of the internet. This is how I have learned most of what I know so far. The most recent thing I've learned are Sockets, if that gives you an idea of what level I'm at.

Context: I am creating a small multiplayer game played through a standalone applet: a 2D shooter where the player(s) have to defeat a barrage of NPC bots using guns and explosives as they progress further into enemy territory. The guns' stats differ throughout the game depending on other variables

Problem: Now that I have made a solid foundation (though still buggy), I want to add audio/sound effects. I don't know how to do this, let alone how to do it efficiently

La pregunta:

What is the optimal way to add sound effects if they play repeatedly (e.g. firing a gun), often before the previous iteration has stopped?
I want to have a single sound for all the different guns (maybe a few sounds that alternate). The rate of fire is not constant. Will I have to call a method for every bullet? Do I have to make a new thread each time I play it? I've searched Google and found some ways to add sound/music, but I'm still not too sure if that's how I should approach this.

If it's too complicated I won't even bother. Thanks in advance.

edit: the guns' firing sounds are not the only sounds. There are various other sfx such as helicopter sounds, rockets launching, rockets travelling, bullets whizzing if they come close to you, explosion, tank noises, parachute sounds, dying sounds, etc. Is this too much? Does having lots of sound impact the game's performance?

irrelevant: why am I not allowed to create new tag(s): sfx, repeating, often?

you will probably want to have each sound in a Sound object (you will have to write it)
and include a method of running it in a seperate thread.

import java.applet.AudioClip;

class SoundPlayer extends Thread
{
    AudioClip sound;
    public SoundPlayer (AudioClip sound)
    {
        this.sound = sound;
    }

    run ()
    {
        sound.play();
        stop();
    }
}

instatiate your audio as a SoundPlayer

SoundPlayer gunSound = new SoundPlayer (gunshotAudioClip);

when you want to play the sound

gunSound.start(); // this will play your sound in a new thread.

that is basically the code I use when i need to use sound effects in a game or other program

Thanks, but I need some more help. The sounds work the first thousand times, but I can't get my class to self-terminate

class soundThread extends Thread
{
    String soundName;
    soundThread(String i_soundName)
    {
        soundName = i_soundName;
    }
    public void run()
    {
        try{
            AudioInputStream stream = AudioSystem.getAudioInputStream(ClassLoader.getSystemClassLoader().getResource(soundName));
            DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
            Clip clip = (Clip)AudioSystem.getLine(info);
            clip.open(stream);
            clip.start();
            Thread.currentThread().interrupt();
            return;
        }catch(Exception e){System.out.println("err= soundThread_error");e.printStackTrace();return;}
    }
}

I misunderstood.

The problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.