libmpv for Android

mpv for Android, as one dependency.

mpv, FFmpeg and libplacebo, built for every Android ABI and published to Maven Central with a small Kotlin wrapper. It's the player engine behind Ephyra.

Latest release: see Releases. GPL-3.0-or-later. Android 8.0 or newer.

build.gradle.kts

dependencies {
    implementation("io.github.midasvo:libmpv:<version>")
}

Kotlin

import dev.jdtech.mpv.MPVLib

val mpv = MPVLib.create(context) ?: return
mpv.setOptionString("gpu-context", "android")
mpv.setOptionString("hwdec", "mediacodec")
mpv.init()

// once the Surface exists
mpv.attachSurface(holder.surface)
mpv.setOptionString("force-window", "yes")
mpv.setOptionString("vo", "gpu")
mpv.command(arrayOf("loadfile", url))

// when the player goes away
mpv.destroy()

Use it

It's on Maven Central, so there's no extra repository to add. One AAR holds the native libraries for arm64-v8a, armeabi-v7a, x86 and x86_64, and the Kotlin wrapper MPVLib.

New project

// settings.gradle.kts: mavenCentral() is usually there already
dependencies {
    implementation("io.github.midasvo:libmpv:<version>")
}

The wrapper creates an mpv instance, attaches it to a Surface, sets options and properties, sends commands, and passes mpv's events and log messages to your observers. Callbacks arrive on mpv's event thread. Everything else is mpv itself: its manual is the reference for options, properties and commands.

Coming from dev.jdtech.mpv:libmpv

Change the coordinate and nothing else. The Kotlin package stays dev.jdtech.mpv, so your code compiles unchanged. Don't keep both: they ship the same native libraries under the same file names.

Two things changed in mpv 0.42 that a player notices:

  • http(s) goes through libcurl by default. curl-enabled=no brings back FFmpeg's own networking.
  • tls-verify defaults to yes, and the build has no CA bundle of its own.

More of mpv's API

Since 1.4.0. Extension functions on MPVLib, in the same package: nothing above changes, and observers get what they got before. The new functions throw MpvException with mpv's error code. The library brings kotlinx-coroutines-core with it.

Structured values

MpvNode is mpv's mpv_node: maps, arrays and typed values instead of JSON text, from getPropertyNode, setPropertyNode and commandNode, which returns a command's result. setPropertyNode throws mpv's error, where the classic setters only log it.

Suspend functions

commandAsync, getPropertyAsync, setPropertyAsync and typed getters such as getDoublePropertyAsync use mpv's own async API, so they never block the calling thread and are safe on the main thread. They can be cancelled, and fail if the instance is destroyed while they wait.

Flows

A flow per property type, and eventFlow() with each event's data. Observing starts and stops with the collection. Property flows keep the latest value; the event flow buffers 256 events per collector, then reports how many it dropped.

Custom protocols, and more

registerStreamProtocol makes mpv read app:// URIs through your Kotlin code: your own HTTP client, a ContentResolver, data in memory. MPVLib is AutoCloseable, and setLogcatLevel keeps mpv's log out of logcat while your observers still get it.

The README has a compiled example of each and says when to prefer them over the classic calls.

What's inside

For each ABI: libmpv.so and the FFmpeg libraries as shared libraries, with everything else statically linked into them, the wrapper libplayer.so, and the NDK's libc++_shared.so. Minimum API 26.

Every version is set in one file, depinfo.sh, and each release's notes list the versions it was built with.

The component table is generated when the page is deployed. Until then: the versions on main are in depinfo.sh, those of a release in its release notes, and the licences in the README.

Released, and next

The page is built from main, which is ahead of the latest release. This is what each has.

In 1.4.0, the latest release

The first release built by the new pipeline. The AAR is signed, has a provenance attestation and an SBOM, and the release attaches its corresponding source. Since 1.3.0:

  • A JNI wrapper in Rust. Same Kotlin API. A call after destroy() throws IllegalStateException instead of touching freed memory, a call racing destroy() is safe, and a second destroy() does nothing.
  • destroy() from a callback throws (call it from another thread), and a null string argument throws NullPointerException.
  • More of mpv's API (above): structured values, suspend functions, flows, custom protocols, unobserveProperty, AutoCloseable and setLogcatLevel. All additive.
  • An mpv patch that rounds the AudioTrack buffer to whole frames, so 22,050 Hz audio is no longer silent.
  • An FFmpeg patch so https works when the server asks for an optional client certificate (Caddy verify_if_given, nginx ssl_verify_client optional).
  • A new build pipeline. Releases built by it come with an SBOM and a provenance attestation.

On main, in the next release

Nothing a user would notice beyond 1.4.0 yet.

Why a fork

This is a fork of jarnedemeulemeester/libmpv-android, the library Findroid uses. Upstream had already merged mpv 0.42 and FFmpeg 9, but there was no release with them yet, and Ephyra needed the libplacebo fix for AV1 film grain. So this fork publishes its own releases. It has since grown its own wrapper and build pipeline, and still takes upstream's changes.

Follows upstream

A weekly workflow opens a pull request that merges upstream's main. Where the fork has its own version of a file, such as the wrapper, the change is ported by hand.

Current

Renovate proposes every new version of mpv, FFmpeg and the rest. CI builds all four ABIs on each one before it can be merged.

Tested

CI plays a real file on three emulators (Android 8.0, a 32-bit x86 one and a current one) and checks which decoders, demuxers and protocols the build contains. A release goes into Ephyra before anything else: 1.1.0 was played on a phone, on emulators and on a 32-bit Android TV box, including hardware-decoded 10-bit AV1.

Licence and source

The code in the repository is MIT. The AAR is effectively GPL-3.0-or-later, because libmpv.so links FFmpeg and other GPL components. An app that ships it has to show the licences and offer the source.

Source with every release

Each release attaches libmpv-<version>-sources.tar.xz: the repository at the tag plus every dependency as downloaded and patched. Releases after 1.3.0 also hold the wrapper's Rust crates. Link to it from your app and that part is covered.

Signed and checked

Releases are signed with a dedicated key, DD02 C554 A0EB AF24 B72B 68B0 D84A 55E8 003C 6AB5, published on keys.openpgp.org and keyserver.ubuntu.com. The AAR on a release is the file on Maven Central. The build leaves the date out of libmpv.so, and CI bundles the AAR twice in separate jobs and fails unless the two are identical.

Verify a release

# the signature (every release)
gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys DD02C554A0EBAF24B72B68B0D84A55E8003C6AB5
gpg --verify libmpv-<version>.aar.asc libmpv-<version>.aar

# where it was built (releases after 1.3.0)
gh attestation verify libmpv-<version>.aar -R midasvo/libmpv-android

The attestation shows that the file was built by this repository's release workflow, from which commit and tag. Releases after 1.3.0 also attach an SBOM, libmpv-<version>.cdx.json, with every component's version, licence, source and patches. More in RELEASING.md.