Wav File To Play When Button Pressed
Page 1 of 111 Replies - 4853 Views - Last Post: 06 April 2011 - 09:49 PM
#1
Wav File To Play When Button Pressed
Posted 06 April 2011 - 08:11 AM
I want a small 3 second wav audio file to play when a button is pressed.
The button currently navigates to a new page so I would like it to do both.
I've had a look at AudioInputStreams etc.
But I'm not even sure where to begin.
Anybody help?
thanks
Replies To: Wav File To Play When Button Pressed
#2
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 08:44 AM
//Member variables:
private AudioInputStream clipNameAIS;
private Clip clipNameClip;
//constructor/initialization
try
{
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
}catch(Exception e){System.out.println("Failure to load sound");}
//Action where .wav is played
clipNameClip.setFramePosition(0);
clipNameClip.start();
This post has been edited by Greltam: 06 April 2011 - 08:45 AM
#3
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:18 AM
Greltam, on 06 April 2011 - 08:44 AM, said:
//Member variables:
private AudioInputStream clipNameAIS;
private Clip clipNameClip;
//constructor/initialization
try
{
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
}catch(Exception e){System.out.println("Failure to load sound");}
//Action where .wav is played
clipNameClip.setFramePosition(0);
clipNameClip.start();
Hi,
thanks for that..I tried it just there and it still won't work.
I'm using NetBeans.
Should I make a new Java file called AudioInputStream or where do I put it?
Also, for the :
//Action where .wav is played clipNameClip.setFramePosition(0); clipNameClip.start();
Is that put separately into to Action for the button I want?
Thanks
#4
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:22 AM
What specific problems or errors are you encountering? "It doesn't work" doesn't help us.
#5
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:24 AM
P.S. NetBeans is lame.
#6
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:29 AM
AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
You can also do a
AudioSystem.getAudioInputStream(new File(/*location of file*/));
or load it using a URL
AudioSystem.getAudioInputStream(/*URL of sound file*/);
*Think it's just a System URL and not Web URL
This post has been edited by Greltam: 06 April 2011 - 09:34 AM
#7
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:35 AM
Greltam, on 06 April 2011 - 09:24 AM, said:
P.S. NetBeans is lame.
Sorry here#s what I have :
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
/**
*
* @author Cian
*/
public class AudioInputStream {
//Member variables:
private AudioInputStream clipNameAIS;
private Clip clipNameClip;
//constructor/initialization
try
{
try {
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("door.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
} catch (LineUnavailableException ex) {
Logger.getLogger(AudioInputStream.class.getName()).log(Level.SEVERE, null, ex);
}
}catch(Exception e){System.out.println("Failure to load sound");
}
}
I have everything imported as far as I can see.
I have to use NetBeans as that's what we use in college. I'm not a big fan of it either.
Errors come up as soon as try occurs and catch etc.
NetBeans doesn't show specifically whats wrong
This post has been edited by emperorkrunchy: 06 April 2011 - 09:45 AM
#8
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 10:15 AM
Java has phenomenal error messages. Why replace those messages with your own? Just use the e.printStackTrace() method to output the relevant error message that will help you debug.
catch(Exception e){System.out.println("Failure to load sound");
}
You have an error log as well. What are the contents of the error log?
Logger.getLogger(AudioInputStream.class.getName()).log(Level.SEVERE, null, ex);
#9
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 10:37 AM
try
{
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
}
NetBeans suggests surrounding the statements with a try catch which im not sure what is but just creates more errors.
Here it is in full at the moment.
package bankgamejava;
import javax.sound.sampled.*;
/**
*
* @author Cian
*/
public class Clip {
//Member variables:
private AudioInputStream clipNameAIS;
private Clip clipNameClip;
//constructor/initialization
try
{
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
}
#10
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 11:23 AM
you also need a catch statement after your try block, AND your try catch block isn't within a constructor or method within your classes. Floating code doesn't compile.
This post has been edited by Greltam: 06 April 2011 - 11:27 AM
#11
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 04:53 PM
#12
Re: Wav File To Play When Button Pressed
Posted 06 April 2011 - 09:49 PM
Greltam, on 06 April 2011 - 10:44 AM, said:
//Member variables:
private AudioInputStream clipNameAIS;
private Clip clipNameClip;
//constructor/initialization
try
{
clipNameAIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sounds/file.wav"));
clipNameClip = AudioSystem.getClip();
clipNameClip.open(clipNameAIS);
}catch(Exception e){System.out.println("Failure to load sound");}
//Action where .wav is played
clipNameClip.setFramePosition(0);
clipNameClip.start();
Greltam you should make a tutorial about it
Audio in Java applications is always misunderstood
|
|

New Topic/Question
Reply



MultiQuote







|