在Mac上用Java播放声音

时间:2015-11-16 02:26:46

标签: java macos audio

好吧,在我发布任何代码之前,这是我的规格:

  • 电脑:Macbook Air,2014年初型号
  • Java版本:Java 8,更新65(Java控制面板说我正在使用推荐版本)
  • 操作系统版本:OS X El Capitan,10.11.1

现在已经不在了,我使用Terminal作为控制台窗口,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 {

        }
    }

}

WHERE ...

  • javacoffeeadventure是软件包的顶级名称
  • commandinterpreter.Console是一种使用控制台快速完成工作的方法(我必须使用全名,因为java.io.Console已经存在)
  • javacoffeeadventure.io.FileIO是处理资源和加载/保存文件的简单方法
  • 我正在播放wav个文件

无论哪种方式,我都会使用javax.sound.sampled.AudioInputStreamjavax.sound.sampled.AudioSystemjavax.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.

1 个答案:

答案 0 :(得分:3)

这是因为OpenJDK绑定到openal-soft,目前它依赖于Carbon。

OpenJDK存在未解决的问题: https://bugs.openjdk.java.net/browse/JDK-8138754

已在 openal-soft 分支中修复: https://github.com/kcat/openal-soft/issues/20

openal-soft 1.17.0 开始,它还没有发布,这是我写的最新版本。

应该可以构建一个较新的openal-soft安装来解决这个问题。它可能已经在Homebrew中提供。

您可以从源代码构建,但我建议您将代码基于Java Sound API example,并将此消息记录为已知问题,因为 openal-soft 已修复且其版本为在你的控制之外。