Results 1 to 13 of 13
Thread: playing wav files
- 02-01-2012, 02:52 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
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!
- 02-01-2012, 03:27 PM #2
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!
- 02-02-2012, 02:16 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
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
- 02-02-2012, 02:32 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: playing wav files
PlaySound.class
- 02-02-2012, 02:41 PM #5
Re: playing wav files
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.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-02-2012, 03:07 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
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
- 02-02-2012, 03:11 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-02-2012, 03:21 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2012, 03:29 PM #9
Re: playing wav files
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!
- 02-02-2012, 04:00 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 02-02-2012, 04:38 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2012, 04:48 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: playing wav files
Just hit it a few more times.
- 02-07-2012, 12:12 PM #13
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Playing a WAV or MP3
By meesterpickles in forum New To JavaReplies: 7Last Post: 12-19-2011, 02:59 AM -
Help please, Playing with folders and files.
By paulio2 in forum New To JavaReplies: 38Last Post: 11-17-2011, 02:16 PM -
Playing mp3 files in Ubuntu
By granadajose in forum Advanced JavaReplies: 2Last Post: 09-07-2011, 08:26 PM -
Wav player not playing
By RightGoodEnt in forum CLDC and MIDPReplies: 0Last Post: 02-13-2011, 04:23 PM -
Playing multiple wave files one after the other
By Pals in forum New To JavaReplies: 4Last Post: 02-26-2010, 03:18 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks