Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need help to implement sound in my game  (Read 943 times)
0 Members and 1 Guest are viewing this topic.
Offline Maverick

Senior Newbie





« Posted 2006-02-04 14: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

Junior Member





« Reply #1 - Posted 2006-02-04 15: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

Senior Newbie





« Reply #2 - Posted 2006-02-04 16: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #3 - Posted 2006-02-04 16: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

Offline Maverick

Senior Newbie





« Reply #4 - Posted 2006-02-04 22: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!