Giter Site home page Giter Site logo

audioplayer4j's Introduction

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.

audioplayer4j's People

Contributors

hendriks73 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.