Tuesday, January 1, 2013

Sample Java Code to Play Sound Effect

Sample Java Code to Play Sound Effect

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.)

STEPS

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.

copied snick1.wma to usb key and mounted on my apple macbook

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

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.

found references to build java source code :

Built java class file from code seen here

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 !


Source : jnorthr[dot]wordpress[dot]com

0 comments:

Post a Comment