Results 1 to 6 of 6

Thread: Sounds

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Question Sounds

    Hello.
    This program down below is supposed to play a sound file from the same folder as the program is in, but for some reason the IDE gives me this error on line 6:
    unreported exception java.net.MalformedURLException; must be caught or be declared to be thrown
    Do you know why?

    Java Code:
    import java.net.*;
    import java.applet.*;
    
    public class Bajs{
        public static void main(String[] args){
            URL u = new URL("file:tjena.au");
            AudioClip a = Applet.newAudioClip(u);
            a.play();
        }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,330
    Rep Power
    5

    Default Re: Sounds

    Just like the error says, your URL is malformed. Try

    Java Code:
    URL u = new URL("file:///f:/tjena.au"); // the logical disk letter may be different.
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    my128samuel is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Sounds

    Hello,

    It seems that the URI is miss taped, you have to write it like this file:///your-full-path
    Please take a look at :
    file URI scheme - Wikipedia, the free encyclopedia

    Regards,

  4. #4
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Sounds

    I tried both of your solutions, but they didn't work. I still get the same error :(

    Please take a look at :
    file URI scheme - Wikipedia, the free encyclopedia
    I will :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Sounds

    Use getResource(...) or getResourceAsStream(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,330
    Rep Power
    5

    Default Re: Sounds

    I'm sorry. I mis-read the error message. I thought it was catching a bad URL.
    Try the following:

    Java Code:
    import java.net.*;
    import java.applet.*;
    
    public class Bajs{
       public static void main(String[] args) {
          try {
             URL u = new URL("file///:f:/tjena.au"); // may need to change disk
                                                     // letter
             AudioClip a = Applet.newAudioClip(u);
             a.play();
          }
          catch (MalformedURLException me) {
             me.printStackTrace();
          }
       }
    }
    Regards,
    Jim
    Last edited by jim829; 09-23-2013 at 11:56 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Importing Sounds
    By dylan in forum New To Java
    Replies: 5
    Last Post: 05-19-2013, 10:25 PM
  2. Making Sounds
    By Humphrey Bogart in forum New To Java
    Replies: 1
    Last Post: 09-05-2012, 12:20 AM
  3. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  4. Sounds?
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 08-29-2011, 07:30 PM
  5. Wave Sounds
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-22-2008, 01:44 PM

Posting Permissions

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