`
kiki1120
  • 浏览: 307483 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Java编写PC喇叭发声程序

    博客分类:
  • Java
阅读更多

有时软件需要发出声音来提醒用户,一般是使用Toolkit.getDefaultToolkit().beep(),但只会使音箱发声,没有音箱的PC仍然没有效果。

 

如果想要电脑主板上的喇叭发出声音可以使用jnative,它是开源的,windows和linux下都能用。

 

首先到sourceforge下载jnative.jar,放到工程lib目录下并Add to Build Path,就可以了。

 

附测试程序

public class Beep {

	public static JNative getBeepJnative() throws NativeException {
		return new JNative("kernel32.dll", "Beep");
	}

	public static void beep(JNative jn, int freq, int dwruration)
			throws NativeException, IllegalAccessException {
		jn.setRetVal(Type.VOID);
		jn.setParameter(0, freq);
		jn.setParameter(1, dwruration);
		jn.invoke();
	}

	public static void main(String[] args) throws NativeException,
			IllegalAccessException {
		// 控制声音频率
		int freqs[] = { 523, 587, 659, 698, 784, 880, 998, 1047, 998, 880, 784,
				698, 659, 587, 523 };
		JNative jn = Beep.getBeepJnative();
		// 控制声音时长
		int dwruration = 300;
		for (int freq : freqs) {
			Beep.beep(jn, freq, dwruration);
		}
	}
}

 

分享到:
评论
1 楼 lzf0530 2014-11-17  
亲 执行时有报错,能否将源码以及jar包打包上传一下?

相关推荐

Global site tag (gtag.js) - Google Analytics