Skip to content

hendriks73/audioplayer4j

Repository files navigation

LGPL 2.1 Maven Central Build and Test CodeCov

audioplayer4j

Simply plays audio.

Usage

To play an audio file, simply call the AudioPlayer's play(uri) method:

AudioPlayer.play(someURI);

Playback will start asynchronously.

Here's a more comprehensive example, using the AudioPlayerFactory:

import com.tagtraum.audioplayer4j.*;
import java.util.concurrent.CountDownLatch;
import java.net.URI;

public class AudioplayerExample {
    
    public static void main(final String[] args) throws Exception {
        
        // open a player object for the given URI
        // do so, with try-resource management        
        try (final AudioPlayer player = AudioPlayerFactory.open(new URI(args[0]))) {

            // add a listener, so that we are notified,
            // once playback has stopped.            
            final CountDownLatch finished = new CountDownLatch(1);
            player.addAudioPlayerListener(new AudioPlayerListener() {
                @Override
                public void started(final AudioPlayer audioPlayer, final URI uri) {
                }

                @Override
                public void finished(final AudioPlayer audioPlayer, final URI uri, final boolean endOfMedia) {
                    finished.countDown();
                }
            });

            // start playback        
            player.play();
            
            // wait until playback has finished.        
            finished.await();
        }
    }
}

If you'd like to be notified about playback process, pause events etc., just add a corresponding PropertyListener. Note that all events are posted on the event dispatch thread (EDT).

Installation

audioplayer4j is released via Maven. You can install it using the following dependency:

<dependencies>
    <dependency>
        <groupId>com.tagtraum</groupId>
        <artifactId>audioplayer4j-complete</artifactId>
    </dependency>
</dependencies>

Without further packages, audioplayer4j works very well on macOS and, but has only mediocre codec (audio format) support on other platforms. That said, audioplayer4j plays very well with others. You can play a wide variety of audio formats by installing a suitable javax.sound.sampled package or JavaFX modules. For details, please see below.

Leveraging javax.sound.sampled Packages

The Java sampled sound API uses a service provider architecture, which can be implemented by third parties (see javax.sound.sampled.spi). Any such providers may be picked up and used for playback by audioplayer4j.

Examples are:

  • FFSampledSP, an FFmpeg based provider (Ubuntu, macOS, Windows)
  • CASampledSP, a Core Audio-based provider (macOS only)

For example, to add FFSampledSP, simply use this dependency:

<dependencies>
    <dependency>
        <groupId>com.tagtraum</groupId>
        <artifactId>ffsampledsp-complete</artifactId>
    </dependency>
</dependencies>

Taking Advantage of JavaFX

In order to allow audioplayer4j to utilize JavaFX libraries, you may also want to add the following dependencies:

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-base</artifactId>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
    </dependency>
</dependencies>

Java Module

audioplayer4j is shipped as a Java module (see JPMS) with the name tagtraum.audioplayer4j.

API

You can find the complete API here.