Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

play sound in java

807603Nov 21 2007
hello...
i have some problem with my program that use sound.
i make a program that when the data that user input is wrong then it play a sound.beep.. like that..
my audio code are like this :

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class MsgSound {
public Clip clip;
public AudioInputStream stream;
public MsgSound(){

}
public void play(String file){
try{
stream = AudioSystem.getAudioInputStream(new File(file));
AudioFormat format = stream.getFormat();
format=stream.getFormat();
if(format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
System.out.println("Converting Audio stream format");
stream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,stream);
format = stream.getFormat();
}
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
clip = (Clip) AudioSystem.getLine(info);

clip.open(stream);
clip.start();
clip.loop(0);
clip.drain();

//Thread.sleep(200);
//clip.stop();
// clip.flush();
// clip.close();

} catch (Exception e) {
e.printStackTrace();
}
}
public static void main ( String[] args ){
try{
new MsgSound().play("./sound/register.au");
}catch(Exception e){

}}
}

first time, no problem with it..but after the sound played 5 time,
my program had an error like :
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:506)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1296)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1077)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1167)
at modul.MsgSound.play(MsgSound.java:39).

i wonder what happen with my program?and how can i solve it??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2007
Added on Nov 21 2007
0 comments
233 views