Skip to main content

Control Music Playback

Send playback, track-navigation, muting, and playback-volume commands to a connected device.

Prerequisites

  • The device is connected and ready.
  • The device conforms to DeviceMusicControlAPI.
  • Explicit volume values are between 0 and 100.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Control AIBuds Music skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/control-aibuds-music. Use it to implement Control AIBuds Music in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBuds.xcframework

Import

Swift
import AIBuds

Protocol

The commands are defined by DeviceMusicControlAPI.

Swift
/// The protocol for device music control API.
protocol DeviceMusicControlAPI: DeviceAPI {
    /// Starts or resumes music playback.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func playMusic(_ completion: AIBudsCompletionHandler?)

    /// Pauses the currently playing music.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func pauseMusic(_ completion: AIBudsCompletionHandler?)

    /// Skips to the next track in the playback queue.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func playNextMusic(_ completion: AIBudsCompletionHandler?)

    /// Returns to the previous track in the playback queue.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func playPreviousMusic(_ completion: AIBudsCompletionHandler?)

    /// Increases the playback volume by one step.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func musicVolumeUp(_ completion: AIBudsCompletionHandler?)

    /// Decreases the playback volume by one step.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func musicVolumeDown(_ completion: AIBudsCompletionHandler?)

    /// Mutes the playback volume.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func mute(_ completion: AIBudsCompletionHandler?)

    /// Unmutes the playback volume.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func unmute(_ completion: AIBudsCompletionHandler?)

    /// Sets the playback volume to a specific level.
    /// - Parameters:
    ///   - volume: The desired volume level (0–100).
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func setMusicVolume(
        _ volume: Int,
        completion: AIBudsCompletionHandler?
    )
}

Instance Methods

MethodPurpose
playMusicStart or resume playback.
pauseMusicPause playback.
playNextMusicMove to the next track.
playPreviousMusicMove to the previous track.
musicVolumeUpIncrease playback volume by one step.
musicVolumeDownDecrease playback volume by one step.
muteMute playback.
unmuteUnmute playback.
setMusicVolumeSet playback volume from 0 through 100.

Parameters

ParameterTypeDescription
volumeInt / NSIntegerDesired playback volume from 0 through 100.
completionAIBudsCompletionHandler?Optional completion handler.

Callback Parameters:

NameTypeDescription
successBool / BOOLWhether the command succeeded.
errorNSError?Failure details, or nil on success.

Return Value

These methods return no value directly.

Usage Examples

Playback

Swift
import AIBuds

guard let device = device as? DeviceMusicControlAPI else {
    print("Device does not support music control")
    return
}

device.playMusic { success, error in
    guard success else {
        print("Play failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Playback command succeeded")
}

Playback Volume

Swift
device.setMusicVolume(60) { success, error in
    if !success {
        print(error?.localizedDescription ?? "Volume update failed")
    }
}

Error Handling

Check success for every command and use error for failure details. Validate explicit volume values before calling; the public contract does not promise clamping.

Best Practices

  1. Use Exact SDK Names: Do not substitute generic play, pause, nextTrack, or previousTrack methods.
  2. Debounce Explicit Volume: The SDK Demo delays slider writes to avoid rapid commands.
  3. Keep Volume APIs Distinct: Use this protocol for playback-oriented controls and DeviceVolumeControlAPI for typed device channels.
  4. Update UI After Success: Do not assume a command succeeded when only dispatched.

Notes

  • The protocol does not include a stop command or a separate support property.
  • Track metadata and queue contents are outside this API.