【问题标题】:Play sounds with Java on a Mac在 Mac 上使用 Java 播放声音
【发布时间】: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.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.

【问题讨论】:

  • “是否有可用的解决方法,这样我就可以在不使用不推荐使用的方法并避免不必要的异常的情况下播放声音?” - 目前没有,除非您准备编写您的自己的本机库来做到这一点。近期可能会有更新
  • @MadProgrammer 它实际上已在 openal-soft 中修复。最坏的情况是 ./configure && make && sudo make install :)
  • @AlainO'Dea 查看编辑
  • @DDPWNAGE 感谢您的尝试和反馈。这绝对像是图书馆的问题。 Kcat 上周晚些时候发布了 OpenAL-Soft 1.17.0,但 AudioComponent.h 修复没有成功:kcat.strangesoft.net/openal.html

标签: java macos audio


【解决方案1】:

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

OpenJDK 上有一个未解决的问题: https://bugs.openjdk.java.net/browse/JDK-8138754

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

openal-soft 1.17.0 尚未发布,这是我撰写本文时可用的最新版本。

应该可以安装一个更新的 openal-soft 来解决这个问题。它可能已经在 Homebrew 中可用。

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

【讨论】:

  • @DDPWNAGE 现在回答你的问题了吗?
猜你喜欢
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
相关资源
最近更新 更多