| | |
working beep method in java.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 33
Reputation:
Solved Threads: 2
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();
}
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();
}
Great, I have drive-by code snippets, drive-by flu shots, I think I'm set for life
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
•
•
Join Date: Nov 2008
Posts: 33
Reputation:
Solved Threads: 2
•
•
•
•
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?
" 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.
![]() |
Similar Threads
- jtextfield query (Java)
Other Threads in the Java Forum
- Previous Thread: SCJP 5.0 and SCJP 6.0 Mock test - 10 sets
- Next Thread: I/O Binary stream
Views: 728 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






