Java Code Spot has come up with a small Music Player Java application that plays wave sound clips. Playing sounds clips in java can be done using the "javax.sound" pakages.
Java Sound supports the following audio file formats: AIFF, AU and WAV.
!!!!!!!!!!....Applet may take some time to load....!!!!!!!!!!
Instruction:1)play button will paly the selected sound clip once.2)Loop Button will play the selected sound clip continuesly.3)Stop Button will stop the playing sound.Note: It takes some time to load sound clip before playing according to your internet speed.
JAVA AudioInputStream:For playing a sound file in Java application we need to create a object of AudioInputStream. AudioInputStream is input stream for audio files like DataInputStream are for data files. An audio input stream is an input stream with a specified audio format and length.
For playing a sound file in Java application we need to create a object of AudioInputStream. AudioInputStream is input stream for audio files like DataInputStream are for data files. An audio input stream is an input stream with a specified audio format and length.
public void selectFile(){ try { URL u = new URL(getCodeBase()+""+playlist.getSelectedItem()); AudioInputStream ai = AudioSystem.getAudioInputStream(u); } catch (Exception e) { e.printStackTrace(); } } |
JAVA Clip Interface:
As AudioInputStream is just an input stream, we cannot directly play them. For playing these java AudioInputStream, Java has a interface Clip in Javax.sound.sampled package.
The Clip interface represents a special kind of data line whose audio data can be loaded prior to playback, instead of being streamed in real time.
Clip c = AudioSystem.getClip(); c.open(ai); c.start(); c.loop(Clip.LOOP_CONTINUOUSLY); c.stop(); c.close(); |
The Clip interface has some functions which can be use to paly audio files.
open() : Java's Clip interface has a open function that takes AudioInputStream as parameter and opens the clip with the format and audio data present in the provided audio input stream. Opening a clip means that it should acquire any required system resources and become operational.
start() : Java's Clip interface has a start function that starts playing the AudioInputStream which is currently open.
loop() : Java's Clip interface has a loop function that continously plays the AudioInputStream which is currently open.
stop() : Java's Clip interface has a stop function that stops the playing AudioInputStream.
close() : Java's Clip interface has a close function that Closes the line and releasese any system resources in use by the line.