簡體   English   中英

在Mac上用Java播放聲音

[英]Play sounds with Java on a Mac

好吧,在我發布任何代碼之前,這是我的規格:

  • 電腦: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 {

        }
    }

}

哪里...

  • javacoffeeadventure是包的頂級名稱
  • commandinterpreter.Console是一種使用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.

這是因為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示例,並將此消息記錄為已知問題,因為openal-soft已得到修復且其版本無法控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM