Skip to content

Commit c2aaf97

Browse files
authored
Merge pull request #157 from kdroidFilter/audio-player
Add audio support
2 parents 09af27a + e8ef165 commit c2aaf97

18 files changed

Lines changed: 2298 additions & 670 deletions

File tree

README.MD

Lines changed: 27 additions & 666 deletions
Large diffs are not rendered by default.

README_AUDIO.MD

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Compose Media Player Audio
2+
3+
[![Maven Central - Audio](https://img.shields.io/maven-central/v/io.github.kdroidfilter/composemediaplayer-audio.svg?label=audio)](https://search.maven.org/artifact/io.github.kdroidfilter/composemediaplayer-audio)
4+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5+
[![Kotlin](https://img.shields.io/badge/kotlin-multiplatform-blue.svg?logo=kotlin)](https://kotlinlang.org/docs/multiplatform.html)
6+
[![Platforms](https://img.shields.io/badge/platforms-Android%20|%20iOS%20|%20macOS%20JVM%20|%20Windows%20JVM%20|%20Linux%20JVM%20|%20Web-lightgrey.svg)](https://github.com/kdroidfilter/ComposeMediaPlayer)
7+
8+
Compose Media Player audio module is a lightweight audio-only player for Compose Multiplatform. It supports Android, iOS, JVM Desktop (macOS/Windows/Linux), and Web (Wasm).
9+
10+
For the video module, see [README_VIDEO.MD](README_VIDEO.MD).
11+
12+
## Table of Contents
13+
- [Features](#features)
14+
- [Installation](#installation)
15+
- [Usage](#usage)
16+
- [State and Controls](#state-and-controls)
17+
- [Error Handling](#error-handling)
18+
- [Platform Notes](#platform-notes)
19+
- [Audio Format Support](#audio-format-support)
20+
- [Sample](#sample)
21+
22+
## Features
23+
24+
- **Multiplatform Support**: Works on Android, iOS, JVM Desktop, and Web (Wasm).
25+
- **Local and Remote Playback**: Play files from disk or stream from HTTP(S) URLs.
26+
- **Simple Controls**: Play, pause, stop, seek, and volume.
27+
- **Live State Helper**: `rememberAudioPlayerLiveState()` exposes state, position, duration, and volume.
28+
- **Error Callbacks**: Hook into playback errors via `setOnErrorListener`.
29+
30+
## Installation
31+
32+
Add the audio module dependency in your `build.gradle.kts`:
33+
34+
```kotlin
35+
dependencies {
36+
implementation("io.github.kdroidfilter:composemediaplayer-audio:<version>")
37+
}
38+
```
39+
40+
## Usage
41+
42+
```kotlin
43+
@Composable
44+
fun AudioSample() {
45+
val audioState = rememberAudioPlayerLiveState()
46+
val url = "https://example.com/stream.mp3"
47+
48+
Row {
49+
Button(onClick = { audioState.player.play(url) }) { Text("Play") }
50+
Button(onClick = { audioState.player.pause() }) { Text("Pause") }
51+
Button(onClick = { audioState.player.stop() }) { Text("Stop") }
52+
}
53+
}
54+
```
55+
56+
For local files, pass a file path or file URI:
57+
58+
```kotlin
59+
val player = AudioPlayer()
60+
player.play("/path/to/file.mp3")
61+
// or: player.play("file:///path/to/file.mp3")
62+
```
63+
64+
If you are using FileKit, you can pass `PlatformFile.getUri()`:
65+
66+
```kotlin
67+
val file = FileKit.openFilePicker(type = FileKitType.File("mp3", "wav"))
68+
file?.let { audioState.player.play(it.getUri()) }
69+
```
70+
71+
## State and Controls
72+
73+
- `currentPosition()` and `currentDuration()` return milliseconds. Duration may be null for live streams.
74+
- `currentPlayerState()` returns `AudioPlayerState.PLAYING`, `PAUSED`, `BUFFERING`, or `IDLE`.
75+
- `seekTo(timeMs)` works only when the source is seekable.
76+
- `setVolume(volume)` expects a value between 0.0 and 1.0.
77+
- `setRate(rate)` is supported on Android/iOS/Web; on JVM it is currently a no-op.
78+
79+
## Error Handling
80+
81+
```kotlin
82+
audioState.player.setOnErrorListener(object : ErrorListener {
83+
override fun onError(message: String?) {
84+
println("Audio error: ${message ?: "Unknown error"}")
85+
}
86+
})
87+
```
88+
89+
## Platform Notes
90+
91+
- Android uses Media3, iOS uses AVFoundation, Web uses HTML5 Audio, JVM uses Rodio.
92+
- Seeking and duration availability depend on the source (stream vs file).
93+
94+
## Audio Format Support
95+
96+
Audio format support depends on OS/browser decoders. The tables below show typical baseline support per backend.
97+
Legend: Yes supported, No not supported, Partial device/browser-dependent.
98+
99+
### JVM (Rodio)
100+
101+
| Format | Local | HTTP | HLS | Seeking |
102+
|--------|-------|------|-----|---------|
103+
| MP3 | Yes | Yes | Yes | Yes |
104+
| AAC | Yes | Yes | Yes | Yes |
105+
| FLAC | Yes | Yes | No | Yes |
106+
| OGG | Yes | Yes | No | Yes |
107+
| WAV | Yes | Yes | No | Yes |
108+
109+
### Android (Media3)
110+
111+
| Format | Local | HTTP | HLS | Seeking |
112+
|--------|-------|------|-----|---------|
113+
| MP3 | Yes | Yes | Yes | Yes |
114+
| AAC | Yes | Yes | Yes | Yes |
115+
| FLAC | Yes | Yes | No | Yes |
116+
| OGG | Yes | Yes | No | Yes |
117+
| WAV | Yes | Yes | No | Yes |
118+
119+
### iOS (AVFoundation)
120+
121+
| Format | Local | HTTP | HLS | Seeking |
122+
|--------|-------|------|-----|---------|
123+
| MP3 | Yes | Yes | Partial | Yes |
124+
| AAC | Yes | Yes | Yes | Yes |
125+
| FLAC | Partial | Partial | No | Partial |
126+
| OGG | No | No | No | No |
127+
| WAV | Yes | Yes | No | Yes |
128+
129+
### Web (HTML5 Audio)
130+
131+
| Format | Local | HTTP | HLS | Seeking |
132+
|--------|-------|------|-----|---------|
133+
| MP3 | Yes | Yes | Partial | Yes |
134+
| AAC | Yes | Yes | Partial | Yes |
135+
| FLAC | Partial | Partial | No | Partial |
136+
| OGG | Partial | Partial | No | Partial |
137+
| WAV | Yes | Yes | No | Yes |
138+
139+
Notes:
140+
- Web "Local" assumes a `blob:` or `file:` URL from a file picker; browser security rules apply.
141+
- Web HLS is supported only in Safari unless you provide a custom HLS loader.
142+
- iOS FLAC support varies by OS version and device.
143+
144+
## Sample
145+
146+
See `sample/composeApp/src/commonMain/kotlin/sample/app/AudioPlayerScreen.kt` for a complete example.

0 commit comments

Comments
 (0)