为什么 Java 不通过蓝牙耳机播放声音?
所以在我的机器上,我的蓝牙工作正常,我可以将音频流传输到它并从中录制,除非我运行有声音的 Java 程序。声音文件通过普通扬声器播放,但不会转发到耳机。我当前的操作系统是 Lubuntu 10.04。
我播放声音的代码是:
public static void playSound(File sound) {
try {
AudioClip cp = Applet.newAudioClip(sound.toURL());
cp.play();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
So on my machine I have Bluetooth working fine, I can stream audio to it and record from it, except when I run a Java program that has sound. The sound files work through regular speakers but they don't get forwarded to the headset. My current operating System is Lubuntu 10.04.
My code to play a sound is:
public static void playSound(File sound) {
try {
AudioClip cp = Applet.newAudioClip(sound.toURL());
cp.play();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Applet.newAudioClip() 方法非常古老。就像旧的 Java 1.0 一样。从那时起,Java 重写了许多完善的 API。我敢打赌,无论播放该声音的代码是什么,都不会考虑操作系统的各种音频设置。 javax.sound.sampled 包具有新的 API,虽然它们更难学习,但它们使您可以更好地控制声音的播放和修改方式。
http://download.oracle.com/javase/tutorial/sound/sampled -overview.html
来测试 Java 是否可以通过蓝牙播放该音频
您可以通过下载http://www.javazoom.net/index.shtml
尝试播放 MP3,看看是否可以通过蓝牙耳机播放。
The Applet.newAudioClip() method is pretty darn old. Like Java 1.0 old. Since then Java has rewritten a lot of it's sound APIs. I bet whatever code is playing that sound doesn't take into account the various audio settings of the OS. The javax.sound.sampled package has the new APIs, and while they are harder to learn, they give you much more control over how the sound is played and modified.
http://download.oracle.com/javase/tutorial/sound/sampled-overview.html
You could test out to see if Java can play that audio over your bluetooth by downloading
http://www.javazoom.net/index.shtml
And try playing an MP3 see if that goes over your bluetooth headset.