How to play .wav files with java
Finally I managed to do the following and it works fine
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class MakeSound {
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
/**
* @param filename the name of the file that is going to be played
*/
public void playSound(String filename){
String strFilename = filename;
try {
soundFile = new File(strFilename);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
}
How to play a wav file using Java?
Just remove the while loop from your code and it should be fixed. The while loop is making the thread sleep so it can't play the audio file.
public void play(Film song) {
try {
Clip sound = AudioSystem.getClip();
sound.open(AudioSystem.getAudioInputStream(new File(song.getClip())));
sound.start();
} catch (Exception e) {
System.out.println("Whatever" + e);
}
}
}
how do I play a wave (wav) file in java
Change your URL declaration , change "file://C:/sound.wav"
to "file:C:/sound.wav"
import java.applet.*;
import java.net.*;
public class MainClass {
public static void main(String[] args) {
try {
AudioClip clip = Applet.newAudioClip(
new URL("file:C:/sound.wav"));
clip.play();
} catch (MalformedURLException murle) {
murle.printStackTrace();
}}}
*I had tested it and working great under NetBeans IDE
Related Topics
How to Convert Strings to and from Utf8 Byte Arrays in Java
Java: Parse Int Value from a Char
Java: Difference Between the Setpreferredsize() and Setsize() Methods in Components
How to Get a List of Dates Between Two Dates in Java
How to Randomly Pick an Element from an Array
Splitting on Comma Outside Quotes
"Code Too Large" Compilation Error in Java
Why Does the Division of Two Integers Return 0.0 in Java
Spring: @Component Versus @Bean
How to Catch Multiple Java Exceptions in the Same Catch Clause
Where Are Static Methods and Static Variables Stored in Java
How to Determine an Object's Class