Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Playing Sound  (Read 2430 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC
« Posted 2012-12-27 09:34:07 »

A game is not so entertaining without sounds. There are three ways of playing sounds in Java.

  • AudioClip class
  • Clip class
  • SourceDataLine

Now let's see each of them in detail.

AudioClip


The main problem with this class is that it is present in the applet package and is not usable for applications. There are also hacks to make it work but it is not our main concern. Let's see an example.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
import java.applet.*;

public class Sound extends Applet {

    public void init(){
        AudioClip sound = getAudioClip(getDocumentBase(), "sound.au");
        sound.play();
    }

}


Another problem with this is that since it is a JDK 1.1 class, it may or may not support wav files (Depends based on machine) but works well with au files.

Clip


It is usable in both applets and in applications but it cannot be used for long lengthed files. Here's an example.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
import javax.sound.sampled.*;

public class Sound {

    public static void main(String[] args){
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("sound.wav"));
        clip.open(inputStream);
        clip.start();
        System.exit(0);
    }

}


You must call
System.exit()
to close any daemon threads which are used by the mixer underneath.

SourceDataLine


It works in all modes and is somewhat advanced (Not the toughest stuff!). Here's an example.

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  
import javax.sound.sampled.*;

public class Sound {

    public static void main(String[] args){
        AudioInputStream in = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("sound.wav"));
        AudioFormat format = in.getFormat();
        // Now create the line info
       DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        // Now get the line
       SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();

        // Time to play the file. We read 512 Kb at once and write it to the line.
       byte[] data = new byte[524288];// 512 Kb
       try {
            int bytesRead = 0;
            while (bytesRead != -1) {
                bytesRead = in.read(data, 0, data.length);
                if (bytesRead >= 0)
                    line.write(data, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Release the resources and close the line
           line.drain();
            line.close();
        }
        // Exit closing daemon threads
       System.exit(0);
    }

}


Hope this helped you playing sounds in java. Thank you for reading my first article.

References



Offline Dane

Senior Newbie





« Reply #1 - Posted 2013-02-14 00:19:14 »

I'm not sure if I'm just confused or not; but isn't that 512kb, not 128kb?

Doing it wrong since 0x7CC.
Offline SHC
« Reply #2 - Posted 2013-02-16 15:47:18 »

Sorry, just mis-commenting. You are right. Changed it.

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.

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

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
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!