Results 1 to 11 of 11
  1. #1
    ProGenius is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Why doesnt the sound play when I change the path?

    Hi. I am trying to play a background sound in my program and so far, after searching and reading lots of posts I finally have a "TaDa" sound playing once when the program starts. But what I want is a sound to be looped everytime I start a program. That sound will be in the same folder as the program.

    This is the code that I put in public static void main() to play the sound:

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    java_testt b = new java_test();
    b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b.startApp();
    }
    });
    }
    private void startApp()
    {playStartupSound();}

    private void endApp() {System.exit(0);}

    private void playStartupSound() {
    Runnable soundPlayer = new Runnable() {
    public void run() {
    try {
    File tadaSound = new File(System.getenv("windir") "/" //This is the windows tada sound. All I want is
    "media/tada.wav"); //to change the path to another sound
    AudioInputStream audioInputStream = AudioSystem
    .getAudioInputStream(new FileInputStream(tadaSound));
    AudioFormat audioFormat = audioInputStream
    .getFormat();
    DataLine.Info dataLineInfo = new DataLine.Info(
    Clip.class, audioFormat);
    Clip clip = (Clip) AudioSystem
    .getLine(dataLineInfo);
    clip.open(audioInputStream);
    clip.start();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    };
    Thread soundPlayingThread = new Thread(soundPlayer);
    soundPlayingThread.start();
    }
    }


    Does anyone know how to choose another sound in the same folder as java_test and make it work with this code because when I change the ath it doesnt run.

    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    What happens when you change the literal substring "tada.wav" to another (existing) file? That should run as well if this version works ...

    kind regards,

    Jos

  3. #3
    ProGenius is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What happens when you change the literal substring "tada.wav" to another (existing) file? That should run as well if this version works ...

    kind regards,

    Jos
    Well it doesnt work because the TaDa file is situated in the Windows installation folder whereas the sound I want to be played is situated in C:\users\azaz\netbeans\3.6\sampledir\welcome.wav and when I change the path it doesnt work. I just need help with the path because I cannot get it to work. The "WINDIR" but confuses me.
    This is the error I get:

    java.io.FileNotFoundException: C:\Windows\media\welcome.wav (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java_assignment$2.run(java_assignment.java:341)
    at java.lang.Thread.run(Thread.java:595)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ProGenius View Post
    Well it doesnt work because the TaDa file is situated in the Windows installation folder whereas the sound I want to be played is situated in C:\users\azaz\netbeans\3.6\sampledir\welcome.wav and when I change the path it doesnt work. I just need help with the path because I cannot get it to work. The "WINDIR" but confuses me.
    This is the error I get:

    java.io.FileNotFoundException: C:\Windows\media\welcome.wav (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java_assignment$2.run(java_assignment.java:341)
    at java.lang.Thread.run(Thread.java:595)
    Well, don't use that path prefix then:

    Java Code:
    File tadaSound = new File("C:/users/azaz/netbeans/3.6/sampledir/welcome.wav");
    kind regards,

    Jos

  5. #5
    ProGenius is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Well after I do that the program runs, the drawing n the 700x700px screen appears but there is no sound and I get this:

    java.io.FileNotFoundException: C:\users\azaz\netbeans\3.6\sampledir\welcome.wav (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java_assignment$2.run(java_assignment.java:341)
    at java.lang.Thread.run(Thread.java:595)

    And also my program is called java_assigmnment but in the file there is also another file called java_assignment$2, why is that?

    Thanks in advance

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ProGenius View Post
    Well after I do that the program runs, the drawing n the 700x700px screen appears but there is no sound and I get this:

    java.io.FileNotFoundException: C:\users\azaz\netbeans\3.6\sampledir\welcome.wav (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java_assignment$2.run(java_assignment.java:341)
    at java.lang.Thread.run(Thread.java:595)

    And also my program is called java_assigmnment but in the file there is also another file called java_assignment$2, why is that?

    Thanks in advance
    Well, if the jvm can't find the file where you told to look for it, it really isn't there; check that complete file path. w.r.t. that java_assignment$2 class: that is the name of an anonymous class (e.g. an ActionListener); it needs to have a name so javac, your compiler, made one for you.

    kind regards,

    Jos

  7. #7
    ProGenius is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Well, if the jvm can't find the file where you told to look for it, it really isn't there; check that complete file path. w.r.t. that java_assignment$2 class: that is the name of an anonymous class (e.g. an ActionListener); it needs to have a name so javac, your compiler, made one for you.

    kind regards,

    Jos
    Hi. Thanks a lot for your help. Is does work now. I have forgotten a . the path is C:\Users\azaz\.netbeans\3.6\sampledir. Do you see the dot between azaz and netbeans? Thats what i missed.
    Can you help me loop the sound so it plays until I close the application?

    Thanks a lot for your help Jos. You really helped me. Thank you.
    Last edited by ProGenius; 12-22-2009 at 02:16 PM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ProGenius View Post
    Hi. Thanks a lot for your help. Is does work now. I have forgotten a . the path is C:\Users\azaz\.netbeans\3.6\sampledir. Do you see the dot between azaz and netbeans? Thats what i missed.
    Can you help me loop the sound so it plays until I close the application?

    Thanks a lot for your help Jos. You really helped me. Thank you.
    You can loop() a clip continuously (see the API documentation for the Clip interface).

    kind regards,

    Jos

  9. #9
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    maybe you can just create a loop, i am not sure but maybe you can do something like this.

    Java Code:
    music = 5;
    if (music <6) {
    //play sound here
    }
    can someone confirm this? but i think that is a way to make a loop.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    maybe you can just create a loop, i am not sure but maybe you can do something like this.

    Java Code:
    music = 5;
    if (music <6) {
    //play sound here
    }
    can someone confirm this? but i think that is a way to make a loop.
    That is not needed: a Clip can loop for itself (see my previous reply).

    kind regards,

    Jos

  11. #11
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

Similar Threads

  1. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  2. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  3. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM
  4. Make sound play in a java application
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 11:45 AM
  5. Play sound and Video in a Mobile Phone
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 2
    Last Post: 05-23-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •