SonixPlayer

Unified audio player for playback with pitch shifting and tempo control.

What is SonixPlayer?

SonixPlayer plays audio files with real-time control over:

  • Pitch: Shift up/down by semitones without affecting speed

  • Tempo: Speed up/slow down without affecting pitch

  • Volume: Control playback volume

  • Looping: Repeat audio any number of times

Use it for:

  • Karaoke apps: Play backing tracks with pitch/tempo adjustment

  • Music practice: Slow down songs to learn parts

  • Audio players: Standard playback with seek and loop

Quick Start

Kotlin

val player = SonixPlayer.create("song.mp3")
player.play()
// Later...
player.pause()
player.release()

Swift

let player = try await SonixPlayer.create(source: "song.mp3")
player.play()
// Later...
player.pause()
player.release()

Usage Tiers

Tier 1: Zero-Config (80% of users)

Kotlin

val player = SonixPlayer.create("song.mp3")
player.play()

Swift

let player = try await SonixPlayer.create(source: "song.mp3")
player.play()

Tier 2: With Config (15% of users)

Kotlin

val config = SonixPlayerConfig.Builder()
.volume(0.8f)
.pitch(-2f) // Lower by 2 semitones
.tempo(0.75f) // 75% speed
.loopCount(3) // Repeat 3 times
.onComplete { println("Done!") }
.build()
val player = SonixPlayer.create("song.mp3", config)

Swift

let config = SonixPlayerConfig.Builder()
.volume(0.8)
.pitch(-2)
.tempo(0.75)
.loopCount(3)
.onComplete { print("Done!") }
.build()
let player = try await SonixPlayer.create(source: "song.mp3", config: config)

Tier 3: Runtime Control (5% of users)

Kotlin

player.pitch = -3f      // Change key at runtime
player.tempo = 0.5f // Slow down to half speed
player.volume = 0.5f // Reduce volume

Platform Notes

iOS

  • Uses AVFoundation under the hood

  • Supports M4A, MP3, WAV, and other system-supported formats

  • Audio session automatically configured for playback

Android

  • Uses ExoPlayer under the hood

  • Supports M4A, MP3, WAV, OGG, and other ExoPlayer-supported formats

  • Audio focus handled automatically

Common Pitfalls

  1. Forgetting to release: Call player.release() to free resources

  2. File not found: Ensure path is accessible (bundle/assets on iOS/Android)

  3. Extreme tempo values: Keep between 0.25 and 4.0 for quality audio

  4. Using after release: Create a new player if you need to play again

See also

Configuration options for playback

For recording audio

CalibraLiveEval

For coordinated playback with live evaluation

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Listener interface for playback events. Alternative to StateFlow observation and Builder callbacks.

Properties

Link copied to clipboard

This player as a PlaybackInfoProvider for recording sync. Use with SonixRecorder.Builder().playbackSyncProvider()

Link copied to clipboard
val currentTime: StateFlow<Long>

Current playback position in milliseconds

Link copied to clipboard

Total duration in milliseconds

Link copied to clipboard
val error: StateFlow<SonixError?>
Link copied to clipboard
val isPlaying: StateFlow<Boolean>

Whether currently playing

Link copied to clipboard

Loop count: 1 = play once, 2 = repeat once, -1 = infinite

Link copied to clipboard

Pitch shift in semitones (-12 to +12)

Link copied to clipboard

Tempo/speed multiplier (0.25 to 4.0, 1.0 = normal speed)

Link copied to clipboard

Volume from 0.0 (silent) to 1.0 (full)

Functions

Link copied to clipboard
suspend fun fadeIn(targetVolume: Float = 1.0f, durationMs: Long = 500)

Fade in to target volume

Link copied to clipboard
suspend fun fadeOut(durationMs: Long = 500)

Fade out to silence

Link copied to clipboard
suspend fun load(path: String): Boolean

Load audio from file path (for Builder without source)

fun load(data: ByteArray, sampleRate: Int = 44100, channels: Int = 1)

Load audio from raw PCM data

Link copied to clipboard
fun pause()

Pause playback

Link copied to clipboard
fun play()

Start or resume playback

Link copied to clipboard
fun release()

Release all resources

Link copied to clipboard
fun seek(positionMs: Long)

Seek to position in milliseconds

Link copied to clipboard

Set a listener for playback events.

Link copied to clipboard

Installs a processing tap to receive and modify audio buffers during playback.

Link copied to clipboard
suspend fun setVolumeSmooth(targetVolume: Float, durationMs: Long, easing: VolumeEasing = VolumeEasing.Linear)

Smoothly transition volume with easing curve.

Link copied to clipboard
fun stop()

Stop playback and reset to beginning