Play sound(wav audio) in Java
Just for fun or for some application need, java provides functionality to play audio file. This is inbuilt in java and do not need any additional library. Following is simple code to play an wav file in java.
Information about technology, IT, gadget & computer
Just for fun or for some application need, java provides functionality to play audio file. This is inbuilt in java and do not need any additional library. Following is simple code to play an wav file in java.
package play_audio;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
/**
*
* @author Yusata Infotech
*/
public class play_audio {
public static void main(String[] args) {
try {
System.out.println("Start");
File f = new File("d://wavs//beep-1.wav");
AudioInputStream audio = AudioSystem.getAudioInputStream(f);
AudioFormat format;
format = audio.getFormat();
SourceDataLine auline;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[524288];
while (nBytesRead != -1) {
nBytesRead = audio.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
auline.write(abData, 0, nBytesRead);
}
}
} catch (Exception E) {
System.out.println(E.getMessage());
}
}
}
0 comments:
Post a Comment