Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: playing wav files

  1. #1
    treeface99 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default playing wav files

    HI all,

    All I am trying to do is learn how to play wav files. I have found a few examples of code that run ok but they are really long and complex and i cant get my head around it.

    Can anyone show/write me very basic code for playing a wav file. Simplest code possible. Or know of any good tutorials?

    The sound i want to play is in my project directory and is called - sound.wav

    Many Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,701
    Rep Power
    8

    Default Re: playing wav files

    Java Code:
    AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));
    Clip music = AudioSystem.getClip();
    music.open(stream);
    music.start();
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    treeface99 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: playing wav files

    Thanks for that, I cant run that as getClass() is non static and I cant make reference to it. How can i access getClass() to make it static?


    import java.io.*;

    import javax.sound.sampled.*;

    public class PlaySound
    {
    public void main(String[] args)
    {
    AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));
    Clip music = AudioSystem.getClip();
    music.open(stream);
    music.start();
    }

    }

    Thanks

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: playing wav files

    PlaySound.class

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,701
    Rep Power
    8

    Default Re: playing wav files

    Quote Originally Posted by treeface99 View Post
    Thanks for that, I cant run that as getClass() is non static and I cant make reference to it. How can i access getClass() to make it static?
    If you really couldn't think of a solution to that problem, perhaps you should wait until you're more familiar with the basics before you try to do things like play sounds. I'm not trying to be mean or discourage you, but it's not usually a good idea to dive into the deep end before you learn how to swim.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    treeface99 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: playing wav files

    I have written a speech recognition program for a robot i have built so i would like to think i know the basics however havent programmed in java for 2 years and need to refresh. Static errors have caused me problems in the past and i have corrected them ok.

    does this code require a try and catch or is there something simple i am not catching on to ? :S thanks

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: playing wav files

    Is it complaining about an exception when you're compiling it?
    If not then you don't need it...unless you're dealing with resources that require closing, but I don't know how AudioClip works so I couldn't say if you needed one or not to ensure the stream was closed.

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

    Default Re: playing wav files

    The getClass() method is inherited from the Object class and it isn't a static method indeed. Invoke it from a non-static context, e.g. create an object and you can call that method from there (the usual case).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,701
    Rep Power
    8

    Default Re: playing wav files

    Quote Originally Posted by treeface99 View Post
    I have written a speech recognition program for a robot i have built so i would like to think i know the basics however havent programmed in java for 2 years and need to refresh. Static errors have caused me problems in the past and i have corrected them ok.

    does this code require a try and catch or is there something simple i am not catching on to ? :S thanks
    Like I said, I'm not looking to offend you, so you don't need to defend yourself to me. I'm just saying that if things like static and catching Exceptions are still giving you trouble, then perhaps it's a good idea to spend some more time with the basic tutorials. It's up to you whether you want to follow that advice, but I would bet it's more efficient than posting a question here and waiting on a response every time you're unsure of something.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: playing wav files

    Quote Originally Posted by JosAH View Post
    The getClass() method is inherited from the Object class and it isn't a static method indeed. Invoke it from a non-static context, e.g. create an object and you can call that method from there (the usual case).

    kind regards,

    Jos
    Or just use <Classname>.class if you happen to be in a static context.

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

    Default Re: playing wav files

    Quote Originally Posted by Tolls View Post
    Or just use <Classname>.class if you happen to be in a static context.
    Yeah,well ... duh, I couldn't type that because I have to type on an old (steam driven, wooden) laptop, otherwise I would've done so ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: playing wav files

    Just hit it a few more times.

  13. #13
    treeface99 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Playing a WAV or MP3
    By meesterpickles in forum New To Java
    Replies: 7
    Last Post: 12-19-2011, 02:59 AM
  2. Help please, Playing with folders and files.
    By paulio2 in forum New To Java
    Replies: 38
    Last Post: 11-17-2011, 02:16 PM
  3. Playing mp3 files in Ubuntu
    By granadajose in forum Advanced Java
    Replies: 2
    Last Post: 09-07-2011, 08:26 PM
  4. Wav player not playing
    By RightGoodEnt in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-13-2011, 04:23 PM
  5. Replies: 4
    Last Post: 02-26-2010, 03:18 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
  •