DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   working beep method in java. (http://www.daniweb.com/forums/thread158370.html)

AlbertPi Nov 19th, 2008 3:32 pm
working beep method in java.
 
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();
}

jasimp Nov 19th, 2008 5:54 pm
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?

Ezzaral Nov 19th, 2008 6:03 pm
Re: working beep method in java.
 
Drive-by code snippet.

jasimp Nov 19th, 2008 7:14 pm
Re: working beep method in java.
 
Great, I have drive-by code snippets, drive-by flu shots, I think I'm set for life ;)

stultuske Nov 20th, 2008 6:28 am
Re: working beep method in java.
 
Quote:

Originally Posted by Ezzaral (Post 739953)
Drive-by code snippet.

that's what you get in 'tha H00d'

AlbertPi Nov 20th, 2008 10:54 am
Re: working beep method in java.
 
Quote:

Originally Posted by jasimp (Post 739946)
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.


All times are GMT -4. The time now is 6:17 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC