【发布时间】:2016-05-04 20:00:43
【问题描述】:
好的,在我发布任何代码之前,这是我的规格:
- 计算机:Macbook Air,2014 年初型号
- Java 版本:Java 8,Update 65(Java 控制面板显示我使用的是推荐版本)
- 操作系统版本:OS X El Capitan,10.11.1
现在不碍事了,我使用终端作为控制台窗口,java 运行,javac 编译。我的项目中有一个专门用于播放声音的课程。它的完整代码如下:
package javacoffeeadventure.audiomanager;
import java.io.*;
import javax.sound.sampled.*;
import javacoffeeadventure.io.FileIO;
import javacoffeeadventure.commandinterpreter.*;
public class AudioManager {
public static void playSound(String soundName) {
try {
FileIO f = new FileIO();
AudioInputStream ain = AudioSystem.getAudioInputStream(f.getURLForResource("sounds/" + soundName));
Clip clip = AudioSystem.getClip();
clip.open(ain);
clip.start();
} catch (Exception ex) {
javacoffeeadventure.commandinterpreter.Console.sharedConsole().error("Exception while playing sound " + soundName + ": " + ex.getMessage());
} finally {
}
}
}
在哪里...
-
javacoffeeadventure是包的顶级名称 -
commandinterpreter.Console是一种使用控制台快速执行操作的方法(我必须使用全名,因为java.io.Console已经存在) -
javacoffeeadventure.io.FileIO是一种处理资源和加载/保存文件的简单方法 - 我正在播放
wav文件
无论哪种方式,我都在使用javax.sound.sampled.AudioInputStream、javax.sound.sampled.AudioSystem 和javax.sound.sampled.Clip 来播放声音。
调用clip.start()时出现如下错误:
2015-11-15 21:23:08.195 java[77192:2681680] 21:23:08.195 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
这告诉我 Java 使用过时的框架 CarbonComponent.h 来播放声音,OS X 建议 Java 转换到 AudioComponent.h。
是否有可用的解决方法,这样我就可以在不使用已弃用的方法并避免不必要的异常的情况下播放声音?
编辑
Java Sound API有效,但它在让 Jukebox 播放时输出:
015-11-16 09:23:29.489 java[98123:2879394] 09:23:29.489 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
2015-11-16 09:23:40.572 java[98123:2879867] 09:23:40.572 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
java.lang.IllegalArgumentException: Unsupported control type: Pan
at com.sun.media.sound.AbstractLine.getControl(AbstractLine.java:150)
at Juke.setPan(Juke.java:435)
at Juke.playSound(Juke.java:302)
at Juke.run(Juke.java:410)
at java.lang.Thread.run(Thread.java:745)
2015-11-16 09:23:54.748 java[98123:2879867] 09:23:54.748 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
Unsupported audio file.
2015-11-16 09:24:00.160 java[98123:2879867] 09:24:00.160 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
java.lang.IllegalArgumentException: Unsupported control type: Pan
at com.sun.media.sound.AbstractLine.getControl(AbstractLine.java:150)
at Juke.setPan(Juke.java:435)
at Juke.playSound(Juke.java:302)
at Juke.run(Juke.java:410)
at java.lang.Thread.run(Thread.java:745)
2015-11-16 09:24:09.542 java[98123:2879867] 09:24:09.542 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
【问题讨论】:
-
“是否有可用的解决方法,这样我就可以在不使用不推荐使用的方法并避免不必要的异常的情况下播放声音?” - 目前没有,除非您准备编写您的自己的本机库来做到这一点。近期可能会有更新
-
@MadProgrammer 它实际上已在 openal-soft 中修复。最坏的情况是 ./configure && make && sudo make install :)
-
@AlainO'Dea 查看编辑
-
@DDPWNAGE 感谢您的尝试和反馈。这绝对像是图书馆的问题。 Kcat 上周晚些时候发布了 OpenAL-Soft 1.17.0,但 AudioComponent.h 修复没有成功:kcat.strangesoft.net/openal.html