简体   繁体   中英

Play sounds with Java on a Mac

Alright, before I post any code, here's my specs:

  • Computer: Macbook Air, Early 2014 model
  • Java version: Java 8, Update 65 (Java Control Panel says I'm using the recommended version)
  • OS Version: OS X El Capitan, 10.11.1

Now that that's out of the way, I use Terminal as a Console window, java to run, and javac to compile. I have a class in my project dedicated to playing sounds. The complete code for it is below:

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 {

        }
    }

}

Where...

  • javacoffeeadventure is the top-level name of the package
  • commandinterpreter.Console is a way to do stuff quickly with the Console (I had to use the full name because java.io.Console already exists)
  • javacoffeeadventure.io.FileIO is a simple way to handle resources and load/save a file
  • I'm playing a wav file

Either way, I'm using javax.sound.sampled.AudioInputStream , javax.sound.sampled.AudioSystem , and javax.sound.sampled.Clip to play sounds.

When invoking clip.start() , the following error occurs:

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.

This tells me Java uses an obsolete framework, CarbonComponent.h , to play sounds, and OS X suggests Java transition to AudioComponent.h .

Is there a workaround available so I can play sounds without using deprecated methods and avoiding unnecessary exceptions?


Edit

The Java Sound API works , but it outputs this when letting the Jukebox play:

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.

This is because OpenJDK is binding to openal-soft for this and it currently depends on Carbon.

There is an open issue on OpenJDK: https://bugs.openjdk.java.net/browse/JDK-8138754

It has been fixed in the master branch of openal-soft : https://github.com/kcat/openal-soft/issues/20

It has not been released as of openal-soft 1.17.0 which is the latest release available as I write this.

It should be possible to build an install a newer openal-soft to fix this. It may be already available in Homebrew.

You can build from source, but I recommend basing your code on the Java Sound API example and documenting this message as a known issue for now as openal-soft has been fixed and its release is outside your control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM