11 Replies - 9299 Views - Last Post: 06 April 2011 - 09:49 PM Rate Topic: -----

#1 emperorkrunchy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 04-April 11

Wav File To Play When Button Pressed

Posted 06 April 2011 - 08:11 AM

Hi,
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
Is This A Good Question/Topic? 0
  • +

Replies To: Wav File To Play When Button Pressed

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 08:44 AM

I would use AudioInputStream as well. Here's how I load a .wav 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);
}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

Was This Post Helpful? 2
  • +
  • -

#3 emperorkrunchy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 04-April 11

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:18 AM

View PostGreltam, on 06 April 2011 - 08:44 AM, said:

I would use AudioInputStream as well. Here's how I load a .wav 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);
}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
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,451
  • Joined: 27-December 08

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:22 AM

NetBeans is nothing but a fancy editor.

What specific problems or errors are you encountering? "It doesn't work" doesn't help us.
Was This Post Helpful? 0
  • +
  • -

#5 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:24 AM

Did netbeans give you any errors? AudioInputStream is a java class that is part of the javax.sound.sampled package, so you have to import the packages to use it. It goes in the class where you're using it. Also, the clip.setFrameposition(0) puts your clips position to the beginning, and start() is equivalent to play. SO, if you want the sound to play when you click the button, you should put those where you handle the button click.

P.S. NetBeans is lame.
Was This Post Helpful? 0
  • +
  • -

#6 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:29 AM

And when you load the wav file, there are more than one way to skin the cat. I use getClass().getResourceAsStream because I've been dynamically loading my classes and File loading doesn't work.
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 :P I haven't tried loading sounds off the web*

This post has been edited by Greltam: 06 April 2011 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 emperorkrunchy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 04-April 11

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:35 AM

View PostGreltam, on 06 April 2011 - 09:24 AM, said:

Did netbeans give you any errors? AudioInputStream is a java class that is part of the javax.sound.sampled package, so you have to import the packages to use it. It goes in the class where you're using it. Also, the clip.setFrameposition(0) puts your clips position to the beginning, and start() is equivalent to play. SO, if you want the sound to play when you click the button, you should put those where you handle the button click.

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

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,451
  • Joined: 27-December 08

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 10:15 AM

I'm not saying anything good or bad about NetBeans. I'm just saying it has no bearing on compiling Java code.

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);  


Was This Post Helpful? 0
  • +
  • -

#9 emperorkrunchy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 04-April 11

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 10:37 AM

I'm not sure how to view the log but I started again from scratch on the file and got to this which is the main problem :


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);

}



Was This Post Helpful? -1
  • +
  • -

#10 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 11:23 AM

How about you try to not rewrite the java classes audioinputstream and clip? name your classes something different. Also, why not just put the code in the program that you need it in? The one where the button makes the sound?

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

Was This Post Helpful? 1
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,451
  • Joined: 27-December 08

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 04:53 PM

Some reading material:
How to Ask Questions the Smart Way
It doesn't work. That's not good enough.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Wav File To Play When Button Pressed

Posted 06 April 2011 - 09:49 PM

View PostGreltam, on 06 April 2011 - 10:44 AM, said:

I would use AudioInputStream as well. Here's how I load a .wav 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);
}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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1