1

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)
Tim B
  • 40,716
  • 16
  • 83
  • 128
informative
  • 53
  • 2
  • 13

3 Answers3

3

If you can play a URL, then I suggest you try the following:

new File(path).toURI().toURL();

Let me know whether it works.

Also according to the javadocs, you can use a File directly. So you do not need a URL. If you get a FileNotFoundException it's either because the path is wrong, the file isn't there or you do not have the right permissions.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
2

The AudioSystem allows you to specifiy a File reference; AudioSystem#getAudioInputStream(File).

This means you can specify the File directly, without first needing to convert it to a URL of some kind...

File file = new File("c:/Users/liam/workspace/playsound/song.wav");
System.out.println("length : " + file.length());
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);

If you take a look at you path references...

  • "c:/Users/liam/workspace/playsound/song.wav"
  • "file:///workspace\\playsound\\song.wav"

There are not the same, it should be more like file://C/Users/liam/workspace/playsound/song.wav or some such.

You should also be using / and not \ in URLs "file://workspace/playsound/song.wav", but has already been stated, it would be simpler to just use File#getURL#getURI

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Your URL lacks your drive letter. You can build a string for a proper file URL that includes a drive letter, or you can ask a File object for a URL, or you can take the advice in another answer.

bmargulies
  • 97,814
  • 39
  • 186
  • 310