跳到主要内容

设置音量

设置明确的音量级别、同时更新三个可写通道,或按设备定义的步长调整一个受支持通道。

前置条件

  • 设备已连接且就绪。
  • 设备支持 DeviceVolumeControlAPI
  • volumeSetCapability 不为 .none
  • 明确设置的音量值位于 0 到 100。

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds

协议

这些方法由 DeviceVolumeControlAPI 定义。

Swift
/// 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?
    )
}

实例方法

方法用途
setVolume将一个受支持通道设置为明确值。
setVolumes同时设置系统提示音、媒体和通话音量。
volumeUp按设备定义的一个步长提高受支持通道。
volumeDown按设备定义的一个步长降低受支持通道。

参数

参数类型描述
volumeTypeDeviceVolumeType.systemPrompt.media.call
valueInt / NSInteger0 到 100 的明确级别。
systemPromptInt / NSInteger0 到 100 的系统提示音级别。
mediaInt / NSInteger0 到 100 的媒体播放级别。
callInt / NSInteger0 到 100 的通话中级别。
completionAIBudsCompletionHandler?命令结束时调用的可选回调。

回调参数:

名称类型描述
successBool / BOOL命令是否成功。
errorNSError?失败详情;成功时为 nil

返回值

这些方法不直接返回值。

使用示例

设置一个通道

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

同时设置三个通道

Swift
device.setVolumes(
    systemPrompt: 50,
    media: 60,
    call: 70
) { success, error in
    if !success {
        print(error?.localizedDescription ?? "Volume update failed")
    }
}

错误处理

调用前验证能力和明确的取值范围。更新 UI 前检查 success,并通过 error 获取失败详情。公开 API 契约未说明会截断越界值。

最佳实践

  1. 为滑块防抖:SDK Demo 在滑块移动后短暂等待,再发送明确值。
  2. 按钮使用步进 API:需要设备决定步长时,使用 volumeUpvolumeDown
  3. 不要伪造本地播放写入:当前没有设置方法接受本地播放音量值。
  4. 刷新确认后的状态:成功后使用 volumesInfodidVolumesChanged

注意事项

  • DeviceVolumeType 的三个可写 case 是系统提示音、媒体和通话。
  • 在 SDK 提供公开写入 API 前,本地播放音量仍为只读;支持加入后需更新本页。