-1

I've been trying to deal with sound on my Applet for a bit now, and Instead of trying all the different methods, what is the best way to play Sound in Java? There are a few requirements:

  • Needs to be able to loop
  • Needs to be able to load a WAV from an archive JAR(I think with the getClass().getResource)
  • Needs to be able to play more than one sound at the same time, and not clip already playing sounds

Thanks you so much for looking at my question and I hope you guys have an answer!

Thanks to the wonderful help I almost have it working with this:

public class MyGame() {
   Clip bullet;


    public void init(){
        try {

        bullet = AudioSystem.getClip();
        URL url2 = this.getClass().getResource("bulletSound.wav");
        AudioInputStream ais2 = AudioSystem.getAudioInputStream( url2 );

        bullet.open(ais2);
        } catch (LineUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
    randomMethodToPlayBullet(){
        bullet.setFramePosition(0);
        bullet.start();
    }
}

The problem is that the bullet sound plays, but if the randomMethodToPlayBullet is called say twice in a row, before the first bullet sound is done, the seonc one doesnt play.

Jeremy Sayers
  • 637
  • 4
  • 10
  • 23
  • I want to help, but is there some part of 'SSCCE' that you are not understanding? An SSCCE does not need 3 instances of clips to show failure to loop 1 instance. There are further elements to it. Please read the article I linked in an earlier post. – Andrew Thompson Aug 24 '12 at 08:25
  • An SSCCE should include imports. It should be either an applet (or servlet) or include a `main()`.. Please read the article again, carefully. – Andrew Thompson Aug 24 '12 at 08:38

3 Answers3

2

The best way to load resources from jar file is to put in the same folder a class and get the resource with .class.getResource(...) or .class.getResourceaAsStream(...) methods:

URL url = ClazzInTheFolderOfMyMidiFile.class.getResource(nameOfMidiFile);

or

InputStream resourceAsStream = ClazzInTheFolderOfMyMidiFile.class.getResourceAsStream(nameOfMidiFile);
Mark
  • 17,887
  • 13
  • 66
  • 93
  • Wait, which method would I want to use? What's the difference between the two lines? – Jeremy Sayers Aug 24 '12 at 07:19
  • I have correct the example, this are two separate variants. You can take the url or directly the stream, depends from API that is used to work with resource. – Mark Aug 24 '12 at 07:24
  • I almost have it working completly! Except I'm doing this to play a bullet sound(Edited in post) but it only plays it once. – Jeremy Sayers Aug 24 '12 at 07:32
  • That is an another Question :) – Mark Aug 24 '12 at 07:58
  • 1
    That is a good way to separate the questions. Play midi from jar-file and play with looping, belong not to the same thema. And this is not good to change the thema of question :) – Mark Aug 24 '12 at 08:01
  • Return please the first question, and create new one with the looping thema. – Mark Aug 24 '12 at 08:03
  • 1
    *"The best way to load resources from jar file is to put in the same folder a class"* I disagree. 1) Often it makes sense to put resources in a separate Jar. That way the resources or classes can be updated separately and different resource Jars can be supplied for different application looks. 2) It the app. is trusted and the resources use the same structure, they will need to be signed as well. 3) That leads to a security error along the lines of 'resource in same package are signed by different signers' -- Instead, put them in whatever path best suites them and prefix the path with `/`. – Andrew Thompson Aug 24 '12 at 08:20
  • *"change the thema of question"* I missed that comment. Good one! I was trying to communicate that to the OP earlier. I thought the message had got through. :( – Andrew Thompson Aug 24 '12 at 08:39
1

The answer for:

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • That looks perfect for playing them, but would you mind real quick explaing how I would load them from the JAR instead of by a URL? – Jeremy Sayers Aug 24 '12 at 07:14
  • Ok so I updated my post with what I currently have now, and now I just have one last problem, noted in the post. – Jeremy Sayers Aug 24 '12 at 07:35
1

You can't play the same Clip twice at the same time. You have to create another instance of Clip to play the sound twice at the same time.

Note that there will be a limit how many clips you can play, so the clip API may not be suited to support a sound-heavy game.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Thank you very much for that, what should I use instead? – Jeremy Sayers Aug 24 '12 at 15:31
  • Have a look at package java.sound.sampled (thats what Clip is using under the hood). Be warned that it requires much more boilerplate code to get things done and may not be immediately intuitive to use (at the advantage of greater flexibility, e.g. balance and volume control). It also allows you to generate sound programmtically (e.g. mix multiple sounds onto the same channel). As long as you're happy with Clip its much simpler to use, depends on how much control over sound you need... – Durandal Aug 24 '12 at 15:39