Results 1 to 3 of 3
  1. #1
    memcuch is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Sound i Java Application

    Hi, I am trying to add sounds to my simple game in Java. The code looks like:

    Java Code:
    import java.io.File;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    .
    .
    .
    .
    public void setHit()
    {
            if(!hit)
            {
                hit=true;
                playSound(new File("sounds/balloon_boom.wav"));
            }
       }
    And it doesn/t work. I get an error:
    Java Code:
     
    error: cannot find symbol playSound(new File("sounds/balloon_boom.wav"));
    symbol:   method playSound(File)
    location: class Menu
    What am I doing wrong and how I can "fix" it?

    Best regards

    //mistake in topic ;/

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Sound i Java Application

    What am I doing wrong and how I can "fix" it?
    Whenever you call a method, you need to prefix the method name with an object or a class name.

    object.playSound();

    or

    ClassName.playSound(); // for a static method

    The only time that you could call a method without prefixing it with an object or a class name is if you defined that method in the current class which isn't the case here.

    I did get the following code to work:

    Java Code:
    import  sun.audio.*;    //import the sun.audio package
    import  java.io.*;
    
    public class Sounds {
    
    	public static void main(String[] args) throws IOException {
    
    		// Open an input stream  to the audio file.
    		InputStream in = new FileInputStream("pop.wav");
    
    		// Create an AudioStream object from the input stream.
    		AudioStream as = new AudioStream(in);         
    
    		// Use the static class member "player" from class AudioPlayer to play clip.
    		AudioPlayer.player.start(as);
    	}
    
    }

  3. #3
    memcuch is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Sound i Java Application

    Thanks a lot man, it works!!!

    //ech, not works ;/
    Java Code:
    AudioStream is internal proprietary API and may be removed in a future release
    AudioStream as = new AudioStream(in);
    Last edited by memcuch; 01-11-2013 at 03:23 PM.

Similar Threads

  1. Java Sound
    By cyberia in forum Advanced Java
    Replies: 2
    Last Post: 08-19-2011, 01:58 PM
  2. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 01:02 PM
  3. Make sound play in a java application
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 12:45 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:45 AM
  5. MMAPI sound application
    By luisarca in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-18-2007, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •