943,699 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1579
  • Java RSS
Nov 19th, 2008
0

working beep method in java.

Expand Post »
The following method "sound" works on my PC and it sounds a beep. I am using java 1.5
and Jdeveopler 10.1.3.4 on Windows XP/SP2.

import javax.sound.sampled.*;

public void handleBeep(ReturnEvent returnEvent)
{

try {
sound(2000,150);
} catch (LineUnavailableException lue) {
System.out.println(lue);
}
}


public static void sound(int hz,int msecs) throws LineUnavailableException {
byte[] buf = new byte[msecs*8];

for (int i=0; i<buf.length; i++) {
double angle = i / (8000.0 / hz) * 2.0 * Math.PI;
buf[i] = (byte)(Math.sin(angle) * 80.0);
}

AudioFormat af = new AudioFormat(8000f,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
sdl.write(buf,0,buf.length);
sdl.drain();
sdl.close();
}
Similar Threads
Reputation Points: 9
Solved Threads: 2
Light Poster
AlbertPi is offline Offline
33 posts
since Nov 2008
Nov 19th, 2008
0

Re: working beep method in java.

I'm sorry but are you asking for help? If not, congratulations on getting your beep method to work. If you are asking for help, what is your question?
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 19th, 2008
0

Re: working beep method in java.

Drive-by code snippet.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Nov 19th, 2008
0

Re: working beep method in java.

Great, I have drive-by code snippets, drive-by flu shots, I think I'm set for life
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 20th, 2008
0

Re: working beep method in java.

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Drive-by code snippet.
that's what you get in 'tha H00d'
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Nov 20th, 2008
0

Re: working beep method in java.

Click to Expand / Collapse  Quote originally posted by jasimp ...
I'm sorry but are you asking for help? If not, congratulations on getting your beep method to work. If you are asking for help, what is your question?
I have no issue running this method on my PC, but i have an issue running this on Unix/Weblogic server. I got error message. See below:
" No line matching interface SourceDataLine supporting format PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1 bytes/frame, is supported." from Unix (Sun). Any thought ? Thanks.
Reputation Points: 9
Solved Threads: 2
Light Poster
AlbertPi is offline Offline
33 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: SCJP 5.0 and SCJP 6.0 Mock test - 10 sets
Next Thread in Java Forum Timeline: I/O Binary stream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC