メインコンテンツまでスキップ

音楽再生の操作

接続中のデバイスへ、再生、曲送り、ミュート、再生音量のコマンドを送信します。

前提条件

  • デバイスが接続済みで、操作可能な状態であること。
  • デバイスが DeviceMusicControlAPI に準拠していること。
  • 直接指定する音量が 0〜100 の範囲内であること。

AI を活用して実装

AI で実装

AI でこのワークフローを実装

公式の「AIBuds 音楽再生の操作」スキルを使い、アプリに合わせて実装します。

https://docs-aibuds.github.io/ja/skills/control-aibuds-music を読み、その指示に従ってください。このスキルで「AIBuds 音楽再生の操作」をこの iOS プロジェクトに実装し、検証してください。
公式スキルを見る

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

Swift
import AIBuds

プロトコル

これらのコマンドは 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?
    )
}

インスタンスメソッド

メソッド用途
playMusic再生を開始または再開します。
pauseMusic再生を一時停止します。
playNextMusic次の曲へ移動します。
playPreviousMusic前の曲へ戻ります。
musicVolumeUp再生音量を 1 段階上げます。
musicVolumeDown再生音量を 1 段階下げます。
mute再生音をミュートします。
unmuteミュートを解除します。
setMusicVolume再生音量を 0〜100 で設定します。

パラメータ

パラメータ説明
volumeInt / NSInteger0〜100 で指定する再生音量。
completionAIBudsCompletionHandler?任意の完了ハンドラー。

コールバックのパラメータ:

名前説明
successBool / BOOLコマンドが成功したかどうか。
errorNSError?失敗時の詳細。成功時は nil

戻り値

これらのメソッドは値を直接返しません。

使用例

再生

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")
}

再生音量

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

エラー処理

すべてのコマンドで success を確認し、失敗の詳細は error から取得します。呼び出し前に指定音量を検証してください。公開仕様では範囲外の値が自動補正されることは保証されません。

ベストプラクティス

  1. SDK の正確な名前を使う: 一般的な playpausenextTrackpreviousTrack メソッドへ置き換えないでください。
  2. 音量指定を間引く: SDK Demo では、コマンドの連続送信を避けるためスライダー値の送信を遅延させます。
  3. 音量 API を使い分ける: 再生向け操作にはこのプロトコルを使用し、種類別のデバイス音量には DeviceVolumeControlAPI を使用します。
  4. 成功後に UI を更新する: コマンドを送信しただけで成功したと判断しないでください。

注意事項

  • このプロトコルには停止コマンドや個別の対応状況プロパティはありません。
  • 曲のメタデータと再生キューの内容は、この API の対象外です。