音量の設定
音量を直接指定するほか、設定可能な 3 チャンネルをまとめて更新したり、対応チャンネルをデバイス既定の幅で増減したりできます。
前提条件
- デバイスが接続済みで、操作可能な状態であること。
- デバイスが
DeviceVolumeControlAPIに準拠していること。 volumeSetCapabilityが.noneではないこと。- 直接指定する音量が 0〜100 の範囲内であること。
API リファレンス
フレームワーク
AIBuds.xcframework
インポート
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
これらのメソッドは DeviceVolumeControlAPI で定義されています。
- Swift
- Objective-C
/// The protocol for device volume control API.
protocol DeviceVolumeControlAPI: DeviceAPI {
/// Sets the volume for the specified type.
/// - Parameters:
/// - volumeType: The volume type to adjust.
/// - value: the volume value (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 setVolume(
_ volumeType: DeviceVolumeType,
value: Int,
completion: AIBudsCompletionHandler?
)
/// Sets multiple volume levels at once.
/// - Parameters:
/// - systemPrompt: The system prompt volume level. (0–100)
/// - media: The media playback volume level. (0–100)
/// - call: The call 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 setVolumes(
systemPrompt: Int,
media: Int,
call: Int,
completion: AIBudsCompletionHandler?
)
/// Increases the specified volume channel by one step.
/// - Parameters:
/// - volumeType: The volume channel to adjust.
/// - 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 volumeUp(
_ volumeType: DeviceVolumeType,
completion: AIBudsCompletionHandler?
)
/// Decreases the specified volume channel by one step.
/// - Parameters:
/// - volumeType: The volume channel to adjust.
/// - 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 volumeDown(
_ volumeType: DeviceVolumeType,
completion: AIBudsCompletionHandler?
)
}/// The protocol for device volume control API.
@protocol AIBudsDeviceVolumeControlAPI <AIBudsDeviceAPI>
/// Sets the volume for the specified type.
/// - Parameters:
/// - volumeType: The volume type to adjust.
/// - value: the volume value (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)setVolumeWithType:(enum AIBudsDeviceVolumeType)volumeType
value:(NSInteger)value
completion:(AIBudsCompletionHandler _Nullable)completion;
/// Sets multiple volume levels at once.
/// - Parameters:
/// - systemPrompt: The system prompt volume level. (0–100)
/// - media: The media playback volume level. (0–100)
/// - call: The call 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)setVolumesWithSystemPrompt:(NSInteger)systemPrompt
media:(NSInteger)media
call:(NSInteger)call
completion:(AIBudsCompletionHandler _Nullable)completion;
/// Increases the specified volume channel by one step.
/// - Parameters:
/// - volumeType: The volume channel to adjust.
/// - 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)volumeUpWithType:(enum AIBudsDeviceVolumeType)volumeType
completion:(AIBudsCompletionHandler _Nullable)completion;
/// Decreases the specified volume channel by one step.
/// - Parameters:
/// - volumeType: The volume channel to adjust.
/// - 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)volumeDownWithType:(enum AIBudsDeviceVolumeType)volumeType
completion:(AIBudsCompletionHandler _Nullable)completion;
@endインスタンスメソッド
| メソッド | 用途 |
|---|---|
setVolume | 対応する 1 チャンネルを指定値に設定します。 |
setVolumes | システム音声、メディア、通話中の音量をまとめて設定します。 |
volumeUp | 対応チャンネルをデバイス既定の 1 段階分上げます。 |
volumeDown | 対応チャンネルをデバイス既定の 1 段階分下げます。 |
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
volumeType | DeviceVolumeType | .systemPrompt、.media、.call のいずれか。 |
value | Int / NSInteger | 0〜100 で指定する音量。 |
systemPrompt | Int / NSInteger | 0〜100 のシステム音声音量。 |
media | Int / NSInteger | 0〜100 のメディア再生音量。 |
call | Int / NSInteger | 0〜100 の通話中音量。 |
completion | AIBudsCompletionHandler? | コマンド完了時に呼び出される任意のコールバック。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | コマンドが成功したかどうか。 |
error | NSError? | 失敗時の詳細。成功時は nil。 |
戻り値
これらのメソッドは値を直接返しません。
使用例
1 チャンネルを設定
- Swift
- Objective-C
import AIBuds
guard let device = device as? DeviceVolumeControlAPI,
device.volumeSetCapability != .none
else {
print("Volume setting is unavailable")
return
}
device.setVolume(.media, value: 60) { success, error in
guard success else {
print("Failed to set media volume: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Media volume updated")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceVolumeControlAPI> device = (id<AIBudsDeviceVolumeControlAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceVolumeControlAPI)] &&
device.volumeSetCapability != AIBudsVolumeSetCapabilityNone) {
[device setVolumeWithType:AIBudsDeviceVolumeTypeMedia
value:60
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Failed to set media volume: %@", error.localizedDescription);
return;
}
NSLog(@"Media volume updated");
}];
}3 チャンネルをまとめて設定
- Swift
- Objective-C
device.setVolumes(
systemPrompt: 50,
media: 60,
call: 70
) { success, error in
if !success {
print(error?.localizedDescription ?? "Volume update failed")
}
}[device setVolumesWithSystemPrompt:50
media:60
call:70
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"%@", error.localizedDescription);
}
}];エラー処理
呼び出し前に対応状況と指定値の範囲を確認します。UI を更新する前に success を確認し、失敗の詳細は error から取得してください。公開 API の仕様では、範囲外の値が自動補正されることは保証されません。
ベストプラクティス
- スライダー入力を間引く: SDK Demo では、スライダー操作後に短時間待ってから値を送信します。
- ボタンには段階調整 API を使う: 増減幅をデバイスに任せる場合は
volumeUpとvolumeDownを使用します。 - ローカル再生音量を疑似的に設定しない: 現行の設定 API はローカル再生音量を受け付けません。
- 確定した状態を再取得する: 成功後は
volumesInfoまたはdidVolumesChangedで状態を更新します。
注意事項
- 設定可能な
DeviceVolumeTypeは、システム音声、メディア、通話中の 3 種類です。 - SDK に公開設定 API が追加されるまで、ローカル再生音量は読み取り専用です。対応追加時にこのページも更新してください。