Sound in Java

Beep function is used to produce any sound In Java. Beep function exists in java.awt.Toolkit class. In this program we used for loop and used a thread which play the sound continuously.

Source Code 


package soundinjava;
import java.awt.Toolkit;
import java.util.Scanner;
public class sound {
public static void main (String args [])
 {
  int a;
  Scanner input = new Scanner (System.in);
  System.out.println(" \t \t \t LEP TUTORIALS \n");
  System.out.println(" Do You Want to Play Sound? \n");
  System.out.println(" 1. Start \n");
  a = input.nextInt();
  if (a ==1)
  {
  for(int i = 0 ; i < 100; i++){
   Toolkit.getDefaultToolkit().beep();
   try{
   Thread.sleep(100);
   }
catch(Exception e)
{
System.out.print("Sorry Sound Can't Play ! \n \n ");   
}
 }
  }
 }
 }

Output of the program

Beep function in java to create the sound


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments:

  1. Why are you using Threads ????
    Is there not working with out using threads ???
    For play sound just use the Beep simple if it is for beginners ??

    ReplyDelete