Results 1 to 10 of 10
  1. #1
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Simple sound emission

    I'll keep this short and sweet: all I need to do is emit a sound. The sound itself isn't really important; a general beep or tone will do.

    I've done some searching, but all I see are articles regarding Java synthesizers and the like. I've looked at this once before and found an API class that allowed for a sound to be emitted, but I can't remember what it was.

    If you're wondering what the context is: I'm just playing a sound for the user to be alerted to something. It's a reaction test. I'm guessing that the sound system would be told to run and then eventually told to stop. I'd like to do that in the middle of a monitored time period (System.currentTimeMillis() x2).

    Any advice? Thanks in advance!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Quote Originally Posted by Tsirist View Post
    The sound itself isn't really important; a general beep or tone will do.
    Java Code:
    System.out.println((char)7);

  3. #3
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    System.out.println((char)7);
    This doesn't appear to do anything besides write a box into the console. Thoughts?

    Thanks!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    How are you running the code? When I run it using the command line it emits a beep.

  5. #5
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    How are you running the code? When I run it using the command line it emits a beep.
    Sorry, I suppose I could have included those details. I'm writing and running the code in the Eclipse environment. Here's the code itself:
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class AuditoryReaction
    {
    	public static void main (String [] args) throws InterruptedException
    	{
    		System.out.print(" ");
    		long startTime = 0;
    		long endTime = 0;
    		Thread thread = new Thread();
    		Random generator = new Random();
    		Scanner keyboard = new Scanner(System.in);
    		
    		double timeToStart = 5.0 + (generator.nextDouble() * 10);
    		thread.sleep((long)(timeToStart * 1000));
    		
    		startTime = System.currentTimeMillis();
    		keyboard.nextLine();
    		endTime = System.currentTimeMillis();
    		
    		System.out.println((endTime - startTime) + " milliseconds");
    	}
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    I did a bit of eperimenting. Looks like it doesn't work in a Jar file and probably wouldn't work in a GUI either. Looks like your only option is the delve in the Sound API.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,045
    Rep Power
    23

    Default

    Toolkit.getDefaultToolkit().beep()

    db

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    I stand corrected.

  9. #9
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Awesome, the Toolkit works perfectly. Many thanks to both of you for the help!

  10. #10
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    7

    Default

    Quote Originally Posted by Junky View Post
    How are you running the code? When I run it using the command line it emits a beep.
    Whether a beep is emitted depends on multiple factors, some of which are:
    Terminal application (e.g. Linux VT, Command Prompt, gnome-terminal, xterm)
    BIOS settings
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:02 PM
  2. sound player
    By jperson in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 05:09 AM
  3. Handle sound
    By varsamakos in forum AWT / Swing
    Replies: 4
    Last Post: 12-26-2009, 10:40 PM
  4. add sound to my applet
    By anotsu in forum Java Applets
    Replies: 4
    Last Post: 03-25-2008, 12:26 AM
  5. applet sound
    By marco in forum Java Applets
    Replies: 1
    Last Post: 09-28-2007, 10:31 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
  •