Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Need help to implement sound in my game  (Read 534 times)
0 Members and 1 Guest are viewing this topic.
Offline Maverick

JGO n00b
*

Posts: 12



« on: 2006-02-04 09:25:52 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class SoundPlayer {
    private AudioClip backgroundMusic;
    private AudioClip gunFire;
    private AudioClip hit;
    private AudioClip flak;
    private AudioClip mayday;
    private AudioClip explosion;

    public SoundPlayer() {
        loadAllSounds();
    }

    private void loadAllSounds() {
        try{
            backgroundMusic = Applet.newAudioClip(getClass().getResource("sounds/03 Crush.wav"));
            gunFire = Applet.newAudioClip(getClass().getResource("sounds/shotsingle.wav"));
            hit = Applet.newAudioClip(getClass().getResource("sounds/hit2.wav"));
            flak = Applet.newAudioClip(getClass().getResource("sounds/flak1.wav"));
            mayday = Applet.newAudioClip(getClass().getResource("sounds/mayday.wav"));
            explosion = Applet.newAudioClip(getClass().getResource("sounds/exp1.wav"));

        }catch(Exception e){
        }
    }


My main game panel has a static instance of this soundPlayer called sp. and then the different classes in my game get the SoundPlayer by doing:
PacificFighterPanel.sp.playSound();

the problem is the sometimes the sounds are played, sometimes they are not, and when too many get played it sounds like crap.
Offline g666

Sr. Member
**

Posts: 388



« Reply #1 on: 2006-02-04 10:05:49 »

just createing a new audio clip wont load the sounds. you have to call play on them(and then stop right after). but a better solution would be to look into the java sound api, its not that much more complicated.

desperately seeking sanity
Offline Maverick

JGO n00b
*

Posts: 12



« Reply #2 on: 2006-02-04 11:01:44 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
public class SoundPlayer implements LineListener{
    private Clip clip;
    public SoundPlayer()  {
        clips = new ArrayList<Clip>();
        loadSounds();
    }
    public void playClip(){
        if(clip!=null){
            clip.start();
        }
    }

    public void loadSounds(){
        try{
            AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("sounds/shotsingle.wav"));
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            clip = (Clip) AudioSystem.getLine(info);
            clip.addLineListener(this);
            clip.open(stream);
           
        }catch(UnsupportedAudioFileException e){

        }
        catch(IOException e){

        }catch(LineUnavailableException e){

        }
    }
    public void update(LineEvent event) {
       
        if (event.getType() == LineEvent.Type.STOP) {
            event.getLine().close();
        }
    }
}


exploring a bit the sound api and reading from a book I made this. however, the clip gets played once, and then when it needs to be replayed it does not play. how can I fix this?
Games published by our own members! Go get 'em!
Offline c_lilian

JGO Ninja
***

Posts: 643


Java games will probably rock someday...


« Reply #3 on: 2006-02-04 11:05:58 »

In my games, I never close the line, and use this to trigger a clip :

clip.stop();
clip.setFramePosition(0);
clip.start();

This works fine as long as you don't want the same clip played in parallel.

Lilian

Former java games developer...
Offline Maverick

JGO n00b
*

Posts: 12



« Reply #4 on: 2006-02-04 17:45:39 »

now the clip gets played again if I ask it too, but sometimes it is played correctly and sometimes it isn't. Any ideas?. To explain myself, sometimes I hear the full .wav that is 2 secs long and others I just hear a small beep and nothing more.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.225 seconds with 20 queries.