音楽再生の操作
接続中のデバイスへ、再生、曲送り、ミュート、再生音量のコマンドを送信します。
前提条件
- デバイスが接続済みで、操作可能な状態であること。
- デバイスが
DeviceMusicControlAPIに準拠していること。 - 直接指定する音量が 0〜100 の範囲内であること。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds 音楽再生の操作」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/control-aibuds-music を読み、その指示に従ってください。このスキルで「AIBuds 音楽再生の操作」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBuds.xcframework
インポート
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
これらのコマンドは DeviceMusicControlAPI で定義されています。
- Swift
- Objective-C
/// 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?
)
}/// The protocol for device music control API.
@protocol AIBudsDeviceMusicControlAPI <AIBudsDeviceAPI>
/// 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.
- (void)playMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)pauseMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)playNextMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)playPreviousMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)musicVolumeUpWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)musicVolumeDownWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)muteWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)unmuteWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)setMusicVolume:(NSInteger)volume completion:(AIBudsCompletionHandler _Nullable)completion;
@endインスタンスメソッド
| メソッド | 用途 |
|---|---|
playMusic | 再生を開始または再開します。 |
pauseMusic | 再生を一時停止します。 |
playNextMusic | 次の曲へ移動します。 |
playPreviousMusic | 前の曲へ戻ります。 |
musicVolumeUp | 再生音量を 1 段階上げます。 |
musicVolumeDown | 再生音量を 1 段階下げます。 |
mute | 再生音をミュートします。 |
unmute | ミュートを解除します。 |
setMusicVolume | 再生音量を 0〜100 で設定します。 |
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
volume | Int / NSInteger | 0〜100 で指定する再生音量。 |
completion | AIBudsCompletionHandler? | 任意の完了ハンドラー。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | コマンドが成功したかどうか。 |
error | NSError? | 失敗時の詳細。成功時は nil。 |
戻り値
これらのメソッドは値を直接返しません。
使用例
再生
- Swift
- Objective-C
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")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceMusicControlAPI> device = (id<AIBudsDeviceMusicControlAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceMusicControlAPI)]) {
[device playMusicWithCompletion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Play failed: %@", error.localizedDescription);
return;
}
NSLog(@"Playback command succeeded");
}];
}再生音量
- Swift
- Objective-C
device.setMusicVolume(60) { success, error in
if !success {
print(error?.localizedDescription ?? "Volume update failed")
}
}[device setMusicVolume:60
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"%@", error.localizedDescription);
}
}];エラー処理
すべてのコマンドで success を確認し、失敗の詳細は error から取得します。呼び出し前に指定音量を検証してください。公開仕様では範囲外の値が自動補正されることは保証されません。
ベストプラクティス
- SDK の正確な名前を使う: 一般的な
play、pause、nextTrack、previousTrackメソッドへ置き換えないでください。 - 音量指定を間引く: SDK Demo では、コマンドの連続送信を避けるためスライダー値の送信を遅延させます。
- 音量 API を使い分ける: 再生向け操作にはこのプロトコルを使用し、種類別のデバイス音量には
DeviceVolumeControlAPIを使用します。 - 成功後に UI を更新する: コマンドを送信しただけで成功したと判断しないでください。
注意事項
- このプロトコルには停止コマンドや個別の対応状況プロパティはありません。
- 曲のメタデータと再生キューの内容は、この API の対象外です。