Skip to content

MacFJA/MpvService

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MpvService

MpvService allow you the communicate with Mpv Media Player with the use of JSON-IPC interface.

Installation

Clone the project:

git clone https://github.com/MacFJA/MpvService.git

Install the project into your local Maven repository:

cd MpvService/
mvn clean
mvn install

Remove the source:

cd ..
rm -r MpvService/

Add the dependency in your Maven project:

<project>
    <!-- ... -->
    <dependencies>
        <!-- ... -->
        <dependency>
            <groupId>io.github.macfja</groupId>
            <artifactId>mpv</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!-- ... -->
    </dependencies>
    <!-- ... -->
</project>

Usage

import com.alibaba.fastjson.JSONObject;
import io.github.macfja.mpv.Service;
import io.github.macfja.mpv.communication.handling.PropertyObserver;
import io.github.macfja.mpv.wrapper.Shorthand;

import java.io.IOException;

class App {
    public static void main(String[] args) {
        Shorthand mpv = new Shorthand(new Service());

        // Register the modification of the metadata
        // Indicate that the media changed
        try {
            mpv.registerPropertyChange(new PropertyObserver("metadata") {
                @Override
                public void changed(String propertyName, Object value, Integer id) {
                    JSONObject metadata = (JSONObject) value;
                    System.out.println(String.format(
                            "Playing %s by %s",
                            metadata.getString("title"),
                            metadata.getString("artist")
                    ));
                }
            });
        } catch (IOException e) {
            System.err.println("Unable to register property change!");
        }

        // Add media files to the current playlist
        try {
            mpv.addMedia("path/to/a/media.mp3", true);
            mpv.addMedia("path/another/media.mp3", true);
            mpv.addMedia("path/to/the/third/media.mp3", true);
        } catch (IOException e) {
            System.err.println("Unable to add media!");
        }

        // Start playing
        try {
            mpv.play();
        } catch (IOException e) {
            System.err.println("Unable to start playback!");
        }
        
        // Wait the end of the first file
        mpv.waitForEvent("end-file", 5 * 60 * 1000); // timeout to 5 minutes
    }
}

Other implementation of mpv IPC

License

The MIT License (MIT). Please see License File for more information.

About

Set of classes to manipulate Mpv media player

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages