Sound

Hey,I've looked up info on sound, but everything I've found either doesn't make sense or requires many lines of code in specific places to play a single sound. Can anyone point me to a good tutorial or tell me a simpler way to just play a sound?
[262 byte] By [GilGalvantia] at [2007-11-27 5:31:21]
# 1

No, probably not.

There's java.applet.AudioClip, but that's so basic as to be hardly worth it.

Otherwise this is about as simple as I could make it...

Clip clip = null;

AudioInputStream in = null;

try

{

InputStream tmp = .... // get the plain input stream from wherever

if(tmp != null)

{

in = AudioSystem.getAudioInputStream(tmp);

DataLine.Info clipInfo = new DataLine.Info(Clip.class, in.getFormat());

if(AudioSystem.isLineSupported(clipInfo))

{

clip = (Clip)AudioSystem.getLine(clipInfo);

clip.open(in);

}

}

}

catch(UnsupportedAudioFileException e)

{

System.err.println("Audio file format not supported.");

}

catch(LineUnavailableException e)

{

System.err.println("Audio clip not available.");

}

catch(IOException e)

{

System.err.println("Error creating audio clip.");

}

finally

{

closeStream(in);

}

clip.setFramePosition(0); // rewind, in case it's been played before

clip.loop(0); // play and loop X times

bsampieria at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 2
Alright, thanks, that's a lot better than what I've seen ;).
GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 3
do I need to import anything for that to work? I'm getting an error that class clip doesn't exist :S
GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 4
Of course you need to import stuff. javax.sound.sampled.* at least
bsampieria at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 5
k, it fixed not finding clip, but it can't find input stream now. Also, where should I put this? In the main method? Sorry, I'm new to all of this.
GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 6

Sound is one of the few areas where Java's polite simplicity

doesnt really shine, lol. It requires more code than beginners

are used to for what can be perceived as a trivial task.

There are lots of good sound tutorials online. Its just going to take

a lot of work on your part. Good luck.

TuringPesta at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 7
import java.io.*;
bsampieria at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 8

> Sound is one of the few areas where Java's polite

> simplicity

> doesnt really shine, lol. It requires more code than

> beginners

> are used to for what can be perceived as a trivial

> task.

>

> There are lots of good sound tutorials online. Its

> just going to take

> a lot of work on your part. Good luck.

Yeah, I noticed, heh. I'm used to BASIC where it takes two lines to play a sound. "load sound "filename.wav", soundNum" and "play sound soundNum, startPosition", so it's a big transition having to type 100 lines of code to play a simple sound. Very annoying really, heh :P. I've looked at tutorials, but none have worked, and I have no idea what they are doing. They aren't even using methods, which is the only way I know to do things, and their coding habits are terrible and near impossible to read. I'm incredibly confused, why can't they just make it simpler :S.

GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 9
GAH, I'm still getting that it can't find the classes. Would anyone be kind enough to make me a complete program that runs by itself that just plays a simple .wav file? I have no idea what I'm doing with this.
GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 10

> GAH, I'm still getting that it can't find the

> classes. Would anyone be kind enough to make me a

> complete program that runs by itself that just plays

> a simple .wav file? I have no idea what I'm doing

> with this.

I want you to feel welcome here but I almost feel a bit compelled to

voice my frustration with that post, lol.

I googled "java play wav" and the FIRST result was a usable example:

http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

You really just need to keep at it. Its the only way to learn.

TuringPesta at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 11

> GAH, I'm still getting that it can't find the

> classes. Would anyone be kind enough to make me a

> complete program that runs by itself that just plays

> a simple .wav file? I have no idea what I'm doing

> with this.

I want you to feel welcome here but I almost feel a bit compelled to

voice my frustration with that post, lol.

I googled "java play wav" and the FIRST result was a usable example:

http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

You really just need to keep at it. Its the only way to learn.

TuringPesta at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...
# 12
Sorry about that, I was looking at all the java tutorials from this site. Thanks for the help though :).
GilGalvantia at 2007-7-12 14:56:28 > top of Java-index,Java Essentials,New To Java...