在Mac上使用Java播放声音

在Mac上使用Java播放声音,java,macos,audio,Java,Macos,Audio,好的,在我发布任何代码之前,这里是我的规范: 计算机:Macbook Air,2014年初型号 Java版本:Java8,更新65(Java控制面板说我正在使用推荐的版本) 操作系统版本:OS X El Capitan,10.11.1 现在,这已经不成问题了,我使用终端作为控制台窗口,java运行,javac编译。在我的项目中,我有一个专门播放声音的课程。其完整代码如下所示: package javacoffeeadventure.audiomanager; import java.io.

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

  • 计算机:Macbook Air,2014年初型号
  • Java版本:Java8,更新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
    是包的顶级名称
  • commandexplorer.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
,来播放声音,OSX建议Java过渡到
AudioComponent.h

是否有一种变通方法,可以在不使用不推荐的方法和避免不必要的异常的情况下播放声音


编辑 Java Sound API可以工作,但在让自动存储塔播放时,它会输出以下内容:

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.

这是因为OpenJDK为此绑定到openal soft,目前它依赖于碳

OpenJDK上存在一个未决问题:

它已在openal soft的分支中修复:

它还没有在openal soft 1.17.0发布,这是我写这篇文章时可用的最新版本

应该可以构建一个新的openal软件来修复这个问题。它可能已经在自制中提供


您可以从源代码进行构建,但我建议您将代码基于,并将此消息记录为已知问题,因为openal soft已修复,其发布不在您的控制范围之内。

“是否有解决方法,以便我可以在不使用不推荐的方法和避免不必要的异常的情况下播放声音?”-现在不行,除非您准备编写自己的本机库。在不久的将来可能会有更新。这对你有用吗?@MadProgrammer它实际上是用openal软件修复的。最坏的情况是./configure&&make&&sudo make install:)@AlainO'Dea请参阅edit@DDPWNAGE谢谢你的尝试和回复。这绝对像是图书馆的问题。Kcat上周晚些时候发布了OpenAL Soft 1.17.0,但是AudioComponent.h补丁没有发布:@DDPWNAGE这现在能回答你的问题吗?