gpt4 book ai didi

java - 在 Mac 上使用 Java 播放声音

转载 作者:行者123 更新时间:2023-11-29 04:56:04 24 4
gpt4 key购买 nike

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

  • 计算机: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.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.

最佳答案

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

OpenJDK 上有一个 Unresolved 问题: 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 已得到修复,其发布不在您的控制范围内。

关于java - 在 Mac 上使用 Java 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com