I am trying to play a song from Java. I have tried SO MANY different methods and they all fail giving me errors like this:
Exception in thread "main" java.io.FileNotFoundException: \workspace\playsound\song.wav (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at playsound.LoopSound.main(LoopSound.java:20)
All I need is a simple way to grab the song.wav out of my desktop/src and make it play. This works for the commented out link but not the file:
package playsound;
import java.net.URL;
import javax.sound.sampled.*;
import java.io.File;
public class LoopSound {
public static void main(String[] args) throws Exception {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
File file = new File("c:/Users/liam/workspace/playsound/song.wav");
System.out.println("length : " + file.length());
URL url = new URL("file:///workspace\\playsound\\song.wav");
//"http://www.intriguing.com/mp/_sounds/hg/fart.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(5);
javax.swing.JOptionPane.showMessageDialog(null, "Close to exit!");
}
}
I am sure it is there as i have tested out where it was:
Working Directory = C:\Users\liam\workspace\playsound
And it can tell me how big it is:
length : 2914585
public static void main(String[] args) throws Exception {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
File file = new File("c:/Users/liam/workspace/playsound/song.wav");
System.out.println("length : " + file.length());
new File("workspace\\playsound\\song.wav").toURI().toURL();
This gives me the error:
Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at playsound.musicplayer.main(musicplayer.java:20)