[英]Play sounds with Java on a Mac
好吧,在我發布任何代碼之前,這是我的規格:
現在已經不在了,我使用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.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
來播放聲音,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.