Sample Java Code to Play Sound Effect

NOTE: If your sound clip is MP3, better to read my post for those here.

Intro

Must be brief. Had a need for a bit of java code to play a sound in one of my guis. Found ‘sound recorder’ program under accessories in window 7 on my wife’s system. Had identified a sound bite i could play. How to get it to play on an apple using java code ?

Audio Formats Are A Bother

It seems the format your audio sound clip is stored in, does influence whether you can use java to play it. The oracle javax.sound.sampled.*; package has decoders for sound clips stored in several formats. This sample java code uses javax.sound.sampled.AudioFileFormat.Type choices so only  .WAVE .AU .AIFF .AIFC .SND audio formats are supported – NOT mp3 (see link below for .MP3 sample code so you will need to brew your own if thats what you need, or else convert your sound file into one of the above formats or see my MP3 post here.)

STEPS

1. ran sound recorder with attached webcam microphone to capture the sound from window/7 speakers. This sound was saved in the only choice available for that tool which was the windows audio format .wma and hopefully you wont need to do this agro if you can get your sound clip into a usable format.

2. copied snick1.wma to usb key and mounted on my apple macbook ( actually did a dozen files to confirm it all works, so names range from snick1 thru snick7 )

3. found this tool from an australian group to convert the .wma sound file into another format. see: http://www.nch.com.au/switch/index.html?gclid=CKbEtY6Ex7QCFW3KtAod-TYAlQ

4. downloaded that conversion tool and ran it on my apple mac against my sound file that i named as snick1.wma on windows giving me snick1.wav output. Had to do this as .wav is one of the few audio formats supported by the java package.

5. found references to build java source code :

6. built java class file from code seen here saved as SoundEffect.java

import javax.sound.sampled.*;
import java.net.*;
public class SoundEffect
{
 URL url;
 AudioInputStream ais;
 Clip clip;
 // simple audio file name like snick7.wav works if in same folder as this.class
 // but full filenames like /Volumes/PENDRIVE/groovy/snick7.wav don't 
 public SoundEffect(String fn)
 {
   try
   {
     url = SoundEffect.class.getResource(fn);
     ais = AudioSystem.getAudioInputStream(url);
     clip = AudioSystem.getClip();
     clip.open(ais);
    clip.start(); 
   } 
   catch(Exception e){e.printStackTrace();}
 } // end of constructor

 // one command line argument must be simple audio file name in one of the formats noted above (mp3 NOT supported)
 public static void main(String args[]) throws Exception
 {
 SoundEffect se = new SoundEffect(args[0]);
 }
}

Code Notes

javac SoundEffect.java
java SoundEffect snick7.wav

Will only play  .WAVE .AU .AIFF .AIFC .SND audio formatted audio files in the same directory as this class. Supported file formats can be either 8-bit or 16-bit, with sampling rate from 8 kHz to 48 kHz.

Warning

This sample code is NOT a threaded example so it will tie up your gui thread while it runs; depending on the length of your .wav file, this could be a LONG time !

Wanted to also give you a sample .wav file to play with after you compile this code but wordpress does not allow me to post this sound clip in the .wav format, so i’ve done a funny to add the .jpg to snick7 name so after you download it and put it in the same directory as your compiled class file, you’ll need to rename this sound clip as snick7.wav and when testing this idea, my browser would not let me change the filename suffix on a SAVE AS menu option, so saved with name browser suggested into my target folder, then used a file rename command. If this works and you can play this sound clip, you should hear a really short ‘kachink’ sound  – good luck 🙂

whether you can download this sample .wav file depends on your browser, but try: https://github.com/jnorthr/track6/tree/master/war/resources/snick7.wav

snick7.wav.jpg must be renamed as snick7.wav after you download it.
snick7.wav

One thought on “Sample Java Code to Play Sound Effect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s