playing music using java audioClip
Hi all,
I was thinking of a way to play audio in java. I am using NetBeans, so it did not take me more than five (Ctrl-Space) until I figured out the way.
Today we will discuss creating of simple audio player which can play a (WAV) file.
There are many ways to play audio in java. The method we will use is depending on JavaSoundAudioClip. This method originally supports three audio format, wav, aif and au.
For more formats you may go looking for java sound api examples.
JavaSoundAudioClip implements AudioClip interface with other useful interfaces. This makes things easier for programmers. Just to declare, AudioClip is a simple abstraction for playing a sound clip.
Before jumping to the code, we should design the logic of our program. Our program can has those features:
- User can select audio file to play
- User can play/stop the audio
Simple design can look like this

Code of our program:
In addition to basic java you will need to know how to work with JavaSoundAudioClip.
You can create JavaSoundAudioClip using the following java code:
JavaSoundAudioClip myAudioClip = new JavaSoundAudioClip(new FileInputStream(new File(“pathToFile”)));
Then to start/stop playing the audio you need the following java code:
myAudioClip.play();
myAudioClip.stop();
you may download a copy of the project from here:
SimplePlayer
all the best
Related Posts
- Web page invoker using java
- easy java image Box, my way
- my batch Image Converter using java imageIO
- convert images using java imageIO
- java imageBox (0.0.6)
about 1 year ago
this is very helpful, thanks a lot for the article.
about 1 year ago
Good article. Short and very usefull. Thanks for the simpleplayer code.
about 1 year ago
I bookmarked your site
about 1 year ago
i coudn’t here any sound ..
there is no error ..
what should i do..
i have done in netbeans 5..
but could not here any voice..
can any one help
about 1 year ago
hi Rashid P,
did u check the “SimplePlayer” ?
also be sure to use “wav” files. and to be more sure, include the “wav” file into your project folder.
about 1 year ago
working fine, thanks.