Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/check-alignment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:

- name: Setup Android SDK
uses: android-actions/setup-android@v3
with:
# The legacy "tools" package (the action's default) is no longer published.
packages: platform-tools

- name: Install required tools
run: |
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<p align="center">
<a href="https://android-arsenal.com/api?level=21"><img alt="API" src="https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat"/></a>
<a href="https://jitpack.io/#LottieFiles/dotlottie-android"><img alt="API" src="https://jitpack.io/v/LottieFiles/dotlottie-android.svg"/></a>
<a href="https://github.com/LottieFiles/dotlottie-rs/releases/tag/v0.1.58"><img alt="API" src="https://img.shields.io/badge/dotlottie--rs-0.1.58-blue"/></a>
<a href="https://github.com/LottieFiles/dotlottie-rs/releases/tag/v0.1.60"><img alt="API" src="https://img.shields.io/badge/dotlottie--rs-0.1.60-blue"/></a>
<a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT-yellow.svg"/></a>
</p>
<br />
Expand Down Expand Up @@ -321,6 +321,20 @@ dotLottieAnimationView.loadTheme("theme_id")
dotLottieAnimationView.loadThemeData(themeJsonData)
```

## Audio Support

Animations with embedded audio layers play their sound automatically. Volume can be adjusted on any
player, view, or controller:

```kotlin
dotLottieAnimationView.setAudioVolume(0.5f) // 0f (muted) to 1f (full), default 1f
```

The views and composables hand the application `Context` to the native audio backend for you. If you
drive `com.dotlottie.dlplayer.DotLottiePlayer` directly without a view, call
`com.lottiefiles.dotlottie.core.jni.DotLottiePlayer.ensureAndroidInitialized(context)` once before
loading an animation that contains audio.

## API Reference

#### Playback Control
Expand All @@ -333,6 +347,7 @@ dotLottieAnimationView.loadThemeData(themeJsonData)
- `setPlayMode(Mode)`: Sets playback direction (e.g., `Forward`, `Reverse`).
- `setSegment(Float, Float)`: Sets a specific frame segment to play.
- `setMarker(String)`: Plays the animation between a named marker.
- `setAudioVolume(Float)`: Sets the volume multiplier for audio layers (`0f` to `1f`, default `1f`).

#### Animation State & Properties

Expand All @@ -344,6 +359,7 @@ dotLottieAnimationView.loadThemeData(themeJsonData)
- `currentFrame`: The current frame number.
- `duration`: The total animation duration in seconds.
- `speed`: The current animation speed.
- `audioVolume`: The current audio volume multiplier.
- `loop`: `true` if looping is enabled.
- `loopCount`: The number of times the animation has looped.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class RustBuildPlugin : Plugin<Project> {
"tvg-ttf",
"tvg-otf",
"tvg-lottie-expressions",
"tvg-threads"
"tvg-threads",
"audio"
)
)
extension.abis.convention(listOf("arm64-v8a", "armeabi-v7a", "x86_64", "x86"))
Expand Down
11 changes: 3 additions & 8 deletions dotlottie/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dotlottieRust {
"tvg-otf", // OpenType font support
"tvg-lottie-expressions", // Lottie expression evaluation
"tvg-threads", // multi-threaded rendering
"audio", // audio layers in .lottie files (rodio/oboe)
)

// ─── Optional: build only specific CPU architectures ──────────────────────
Expand All @@ -40,7 +41,7 @@ dotlottieRust {
}

group = "com.lottiefiles"
version = "0.16.0"
version = "0.17.0"

android {
namespace = "com.lottiefiles.dotlottie.core"
Expand All @@ -56,7 +57,7 @@ android {
externalNativeBuild {
cmake {
cppFlags += "-std=c++17"
arguments += listOf("-DANDROID_STL=c++_shared")
arguments += listOf("-DANDROID_STL=c++_static")
}
}

Expand All @@ -79,12 +80,6 @@ android {
}
}

packaging {
jniLibs {
pickFirsts += listOf("**/libc++_shared.so")
}
}

buildTypes {
release {
isMinifyEnabled = false
Expand Down
19 changes: 19 additions & 0 deletions dotlottie/src/main/cpp/jni_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ static jint nativeSetLayout(JNIEnv *env, jclass, jlong ptr, jint fit,
// Config getters
static jint nativeGetMode(JNIEnv *env, jclass, jlong ptr);
static jfloat nativeGetSpeed(JNIEnv *env, jclass, jlong ptr);
static jint nativeSetAudioVolume(JNIEnv *env, jclass, jlong ptr,
jfloat volume);
static jfloat nativeGetAudioVolume(JNIEnv *env, jclass, jlong ptr);
static jboolean nativeGetLoop(JNIEnv *env, jclass, jlong ptr);
static jint nativeGetLoopCount(JNIEnv *env, jclass, jlong ptr);
static jboolean nativeGetAutoplay(JNIEnv *env, jclass, jlong ptr);
Expand Down Expand Up @@ -630,6 +633,20 @@ jfloat nativeGetSpeed(JNIEnv *env, jclass, jlong ptr) {
return dotlottie_get_speed(player);
}

// ==================== Audio ====================

jint nativeSetAudioVolume(JNIEnv *env, jclass, jlong ptr, jfloat volume) {
auto *player = reinterpret_cast<dotlottiePlayer *>(ptr);
return static_cast<jint>(dotlottie_set_audio_volume(player, volume));
}

jfloat nativeGetAudioVolume(JNIEnv *env, jclass, jlong ptr) {
auto *player = reinterpret_cast<dotlottiePlayer *>(ptr);
float volume = 1.0f;
dotlottie_get_audio_volume(player, &volume);
return volume;
}

jboolean nativeGetLoop(JNIEnv *env, jclass, jlong ptr) {
auto *player = reinterpret_cast<dotlottiePlayer *>(ptr);
return dotlottie_get_loop(player) ? JNI_TRUE : JNI_FALSE;
Expand Down Expand Up @@ -1614,6 +1631,8 @@ static JNINativeMethod playerMethods[] = {
// Config getters
{"nativeGetMode", "(J)I", (void *)nativeGetMode},
{"nativeGetSpeed", "(J)F", (void *)nativeGetSpeed},
{"nativeSetAudioVolume", "(JF)I", (void *)nativeSetAudioVolume},
{"nativeGetAudioVolume", "(J)F", (void *)nativeGetAudioVolume},
{"nativeGetLoop", "(J)Z", (void *)nativeGetLoop},
{"nativeGetLoopCount", "(J)I", (void *)nativeGetLoopCount},
{"nativeGetAutoplay", "(J)Z", (void *)nativeGetAutoplay},
Expand Down
2 changes: 1 addition & 1 deletion dotlottie/src/main/cpp/version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dlplayer-version=0.1.58-3bb9d23
dlplayer-version=0.1.60-03cb06ac
api-type=c-api
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ class DotLottieController {
dlplayer?.setTheme(themeId)
}

fun setAudioVolume(volume: Float) {
dlplayer?.setAudioVolume(volume)
}

val audioVolume: Float
get() = dlplayer?.audioVolume() ?: 1f

fun setThemeData(themeData: String) {
dlplayer?.setThemeData(themeData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ class DotLottieDrawable(
scheduleFrame(forceUpdate = true)
}

fun setAudioVolume(volume: Float) {
dlPlayer?.setAudioVolume(volume)
}

val audioVolume: Float
get() = dlPlayer?.audioVolume() ?: 1f

fun setThemeData(themeData: String) {
dlPlayer?.setThemeData(themeData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ class DotLottieAnimation @JvmOverloads constructor(
mLottieDrawable?.setTheme(themeId)
}

fun setAudioVolume(volume: Float) {
mLottieDrawable?.setAudioVolume(volume)
}

val audioVolume: Float
get() = mLottieDrawable?.audioVolume ?: 1f

fun setThemeData(themeData: String) {
mLottieDrawable?.setThemeData(themeData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,15 @@ class DotLottieGLAnimation @JvmOverloads constructor(
}
}

fun setAudioVolume(volume: Float) {
sharedGl.handler.post {
dlPlayer?.setAudioVolume(volume)
}
}

val audioVolume: Float
get() = dlPlayer?.audioVolume() ?: 1f

fun setThemeData(themeData: String) {
sharedGl.handler.post { dlPlayer?.setThemeData(themeData) }
}
Expand Down
Binary file not shown.
Binary file modified dotlottie/src/main/jniLibs/arm64-v8a/libdotlottie_player.so
Binary file not shown.
Binary file not shown.
Binary file modified dotlottie/src/main/jniLibs/armeabi-v7a/libdotlottie_player.so
Binary file not shown.
Binary file removed dotlottie/src/main/jniLibs/x86/libc++_shared.so
Binary file not shown.
Binary file modified dotlottie/src/main/jniLibs/x86/libdotlottie_player.so
Binary file not shown.
Binary file removed dotlottie/src/main/jniLibs/x86_64/libc++_shared.so
Binary file not shown.
Binary file modified dotlottie/src/main/jniLibs/x86_64/libdotlottie_player.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ class DotLottiePlayer {
return (nativeBufferWidth.toLong() * nativeBufferHeight.toLong())
}

// ==================== Audio ====================

/**
* Sets the global volume multiplier for audio layers in the loaded animation.
* The native player clamps [volume] to 0f..1f.
*/
fun setAudioVolume(volume: Float): Boolean {
val result = JNI.nativeSetAudioVolume(nativePtr, volume)
return result == 0
}

fun audioVolume(): Float {
return JNI.nativeGetAudioVolume(nativePtr)
}

// ==================== Theme ====================

fun setTheme(themeId: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ object DotLottiePlayer {
@JvmStatic
external fun nativeGetSpeed(ptr: Long): Float

@JvmStatic
external fun nativeSetAudioVolume(ptr: Long, volume: Float): Int

@JvmStatic
external fun nativeGetAudioVolume(ptr: Long): Float

@JvmStatic
external fun nativeGetLoop(ptr: Long): Boolean

Expand Down
57 changes: 36 additions & 21 deletions make/android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,33 @@ DOTLOTTIE_RS_ROOT ?= deps/dotlottie-rs/dotlottie-rs
MIN_NDK_VERSION = 28

# Default Rust features for Android builds
ANDROID_FEATURES ?= tvg-webp,tvg-png,tvg-jpg,tvg-ttf,tvg-otf,tvg-lottie-expressions,tvg-threads
ANDROID_FEATURES ?= tvg-webp,tvg-png,tvg-jpg,tvg-ttf,tvg-otf,tvg-lottie-expressions,tvg-threads,audio

ifdef FEATURES
ANDROID_FEATURES = $(FEATURES)
endif

# Hardening flags for the C/C++ compiled by cargo build scripts (ThorVG, oboe).
# cargo invokes the NDK clang directly, bypassing ndk-build/CMake, which are
# where the NDK normally injects -fstack-protector-strong. The cc crate appends
# CFLAGS/CXXFLAGS to every compile, so set them here. Rust code itself has no
# stack protector on stable rustc.
ANDROID_HARDENING_FLAGS ?= -fstack-protector-strong

# Link libc++ statically so libc++_shared.so does not have to be shipped.
# The NDK's prebuilt libc++_shared.so has no stack protector and fails
# security scanners (#69). Static libc++ is safe here: only a C API crosses
# the library boundary, ThorVG is built with -fno-exceptions -fno-rtti, and
# the cdylib's version script keeps libc++ symbols out of the export table.
# CXXSTDLIB="" stops both the cc crate and the dotlottie-rs build script from
# adding -lc++ (a linker script for -lc++_shared); the archives are then
# appended explicitly via RUSTFLAGS so they come after every object that
# references them.
ANDROID_CXX_LINK_ARGS ?= -C link-arg=-lc++_static -C link-arg=-lc++abi

# Release and packaging variables
RELEASE_DIR ?= release
ANDROID_RELEASE_DIR ?= $(RELEASE_DIR)/android
LIBCPP_SHARED_LIB ?= libc++_shared.so

# Detect host tag for NDK

Expand Down Expand Up @@ -76,12 +93,6 @@ ANDROID_ABI_x86_64 = x86_64
ANDROID_ABI_x86 = x86
ANDROID_ABI_armv7 = armeabi-v7a

# Android libcpp path mapping
LIBCPP_PATH_aarch64 = aarch64-linux-android
LIBCPP_PATH_x86_64 = x86_64-linux-android
LIBCPP_PATH_x86 = i686-linux-android
LIBCPP_PATH_armv7 = arm-linux-androideabi

# Android compiler mapping
ANDROID_CXX_aarch64 = $(ANDROID_TOOLCHAIN)/bin/aarch64-linux-android$(API_LEVEL)-clang++
ANDROID_CXX_x86_64 = $(ANDROID_TOOLCHAIN)/bin/x86_64-linux-android$(API_LEVEL)-clang++
Expand All @@ -106,7 +117,7 @@ android-check-ndk:
exit 1; \
fi
@if [ -f "$(ANDROID_NDK_HOME)/source.properties" ]; then \
NDK_VERSION=$$(grep "Pkg.Revision" "$(ANDROID_NDK_HOME)/source.properties" | sed 's/.*= *\([0-9]\+\).*/\1/' | head -1); \
NDK_VERSION=$$(grep "Pkg.Revision" "$(ANDROID_NDK_HOME)/source.properties" | sed 's/.*= *\([0-9][0-9]*\).*/\1/' | head -1); \
if [ "$$NDK_VERSION" -lt $(MIN_NDK_VERSION) ]; then \
echo "Error: Android NDK r$(MIN_NDK_VERSION) or higher is required. Found NDK r$$NDK_VERSION"; \
echo "Please upgrade your Android NDK to r$(MIN_NDK_VERSION) or later."; \
Expand Down Expand Up @@ -162,6 +173,10 @@ android-aarch64: android-check-ndk
AR="$(ANDROID_AR)" \
RANLIB="$(ANDROID_RANLIB)" \
BINDGEN_EXTRA_CLANG_ARGS="-isysroot $(ANDROID_TOOLCHAIN)/sysroot" \
CFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXSTDLIB="" \
CARGO_TARGET_AARCH64_LINUX_ANDROID_RUSTFLAGS="$(ANDROID_CXX_LINK_ARGS)" \
cargo rustc \
--manifest-path $(DOTLOTTIE_RS_ROOT)/Cargo.toml \
--crate-type cdylib \
Expand All @@ -172,10 +187,7 @@ android-aarch64: android-check-ndk
@mkdir -p $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_aarch64)
@cp $(DOTLOTTIE_RS_ROOT)/target/$(RUST_TARGET_aarch64)/release/$(ANDROID_LIB_NAME) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_aarch64)/libdotlottie_player.so
@cp $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot/usr/lib/$(LIBCPP_PATH_aarch64)/$(LIBCPP_SHARED_LIB) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_aarch64)/
@if command -v $(ANDROID_STRIP) >/dev/null 2>&1; then \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_aarch64)/$(LIBCPP_SHARED_LIB); \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_aarch64)/libdotlottie_player.so; \
fi
@echo "✓ Android aarch64 build complete"
Expand All @@ -192,6 +204,10 @@ android-x86_64: android-check-ndk
AR="$(ANDROID_AR)" \
RANLIB="$(ANDROID_RANLIB)" \
BINDGEN_EXTRA_CLANG_ARGS="-isysroot $(ANDROID_TOOLCHAIN)/sysroot" \
CFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXSTDLIB="" \
CARGO_TARGET_X86_64_LINUX_ANDROID_RUSTFLAGS="$(ANDROID_CXX_LINK_ARGS)" \
cargo rustc \
--manifest-path $(DOTLOTTIE_RS_ROOT)/Cargo.toml \
--crate-type cdylib \
Expand All @@ -202,10 +218,7 @@ android-x86_64: android-check-ndk
@mkdir -p $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86_64)
@cp $(DOTLOTTIE_RS_ROOT)/target/$(RUST_TARGET_x86_64)/release/$(ANDROID_LIB_NAME) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86_64)/libdotlottie_player.so
@cp $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot/usr/lib/$(LIBCPP_PATH_x86_64)/$(LIBCPP_SHARED_LIB) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86_64)/
@if command -v $(ANDROID_STRIP) >/dev/null 2>&1; then \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86_64)/$(LIBCPP_SHARED_LIB); \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86_64)/libdotlottie_player.so; \
fi
@echo "✓ Android x86_64 build complete"
Expand All @@ -222,6 +235,10 @@ android-x86: android-check-ndk
AR="$(ANDROID_AR)" \
RANLIB="$(ANDROID_RANLIB)" \
BINDGEN_EXTRA_CLANG_ARGS="-isysroot $(ANDROID_TOOLCHAIN)/sysroot" \
CFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXSTDLIB="" \
CARGO_TARGET_I686_LINUX_ANDROID_RUSTFLAGS="$(ANDROID_CXX_LINK_ARGS)" \
cargo rustc \
--manifest-path $(DOTLOTTIE_RS_ROOT)/Cargo.toml \
--crate-type cdylib \
Expand All @@ -232,10 +249,7 @@ android-x86: android-check-ndk
@mkdir -p $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86)
@cp $(DOTLOTTIE_RS_ROOT)/target/$(RUST_TARGET_x86)/release/$(ANDROID_LIB_NAME) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86)/libdotlottie_player.so
@cp $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot/usr/lib/$(LIBCPP_PATH_x86)/$(LIBCPP_SHARED_LIB) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86)/
@if command -v $(ANDROID_STRIP) >/dev/null 2>&1; then \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86)/$(LIBCPP_SHARED_LIB); \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_x86)/libdotlottie_player.so; \
fi
@echo "✓ Android x86 build complete"
Expand All @@ -252,6 +266,10 @@ android-armv7: android-check-ndk
AR="$(ANDROID_AR)" \
RANLIB="$(ANDROID_RANLIB)" \
BINDGEN_EXTRA_CLANG_ARGS="-isysroot $(ANDROID_TOOLCHAIN)/sysroot" \
CFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXFLAGS="$(ANDROID_HARDENING_FLAGS)" \
CXXSTDLIB="" \
CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_RUSTFLAGS="$(ANDROID_CXX_LINK_ARGS)" \
cargo rustc \
--manifest-path $(DOTLOTTIE_RS_ROOT)/Cargo.toml \
--crate-type cdylib \
Expand All @@ -262,10 +280,7 @@ android-armv7: android-check-ndk
@mkdir -p $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_armv7)
@cp $(DOTLOTTIE_RS_ROOT)/target/$(RUST_TARGET_armv7)/release/$(ANDROID_LIB_NAME) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_armv7)/libdotlottie_player.so
@cp $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot/usr/lib/$(LIBCPP_PATH_armv7)/$(LIBCPP_SHARED_LIB) \
$(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_armv7)/
@if command -v $(ANDROID_STRIP) >/dev/null 2>&1; then \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_armv7)/$(LIBCPP_SHARED_LIB); \
$(ANDROID_STRIP) --strip-unneeded $(ANDROID_RELEASE_DIR)/jniLibs/$(ANDROID_ABI_armv7)/libdotlottie_player.so; \
fi
@echo "✓ Android ARMv7 build complete"
Expand Down
Loading