查询媒体数量
向已连接设备请求最新的照片、视频和音频文件数量,然后读取更新后的 mediaCountInfo 属性。
前置条件
查询媒体数量前,请确保:
- 设备已连接并处于稳定状态。
- 设备支持
DeviceInfoAPI协议。
API 参考
框架
AIBuds.xcframework
导入
在需要使用 SDK 的文件中导入主框架:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
请求方法和结果属性定义在 DeviceInfoAPI 中。
- Swift
- Objective-C
protocol DeviceInfoAPI: DeviceAPI {
/// Media file count information, including counts for photos, videos,
/// audio, etc.
var mediaCountInfo: MediaCountInfoModel? { get }
/// Request to query the device media count information.
/// - Parameters:
/// - 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 requestQueryMediaCountInfo(
_ completion: AIBudsCompletionHandler?
)
}@protocol AIBudsDeviceInfoAPI <AIBudsDeviceAPI>
/// Media file count information, including counts for photos, videos,
/// audio, etc.
@property(nonatomic, readonly, strong) AIBudsMediaCountInfoModel *_Nullable mediaCountInfo;
/// Request to query the device media count information.
/// - Parameters:
/// - 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)requestQueryMediaCountInfoWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
@end实例方法
请求设备中存储媒体的最新数量。
- Swift
- Objective-C
/// Request to query the device media count information.
/// - Parameters:
/// - 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 requestQueryMediaCountInfo(
_ completion: AIBudsCompletionHandler?
)/// Request to query the device media count information.
/// - Parameters:
/// - 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)requestQueryMediaCountInfoWithCompletion:(AIBudsCompletionHandler _Nullable)completion;参数
| 参数 | 类型 | 描述 |
|---|---|---|
completion | AIBudsCompletionHandler? | 请求结束时调用的可选完成回调。 |
回调参数:
| 名称 | 类型 | 描述 |
|---|---|---|
success | Bool / BOOL | 请求成功时为 true,否则为 false。 |
error | NSError? | 请求失败时的错误详情;成功时为 nil。 |
结果属性:
| 属性 | 类型 | 描述 |
|---|---|---|
photoCount | NSNumber | 照片数量。 |
videoCount | NSNumber | 视频数量。 |
audioCount | NSNumber | 音频文件数量。 |
返回值
该方法不会在完成回调中返回媒体数量。请求成功后,请读取更新后的 mediaCountInfo 属性。
使用示例
- Swift
- Objective-C
import AIBuds
final class DeviceManager {
weak var device: DeviceConvertible?
func queryMediaCount() {
guard let device = device as? DeviceInfoAPI else {
print("Device does not support media-count queries")
return
}
device.requestQueryMediaCountInfo { success, error in
guard success else {
print("Media-count query failed: \(error?.localizedDescription ?? "Unknown error")")
return
}
guard let counts = device.mediaCountInfo else {
print("The device did not provide media-count information")
return
}
print("Photos: \(counts.photoCount)")
print("Videos: \(counts.videoCount)")
print("Audio files: \(counts.audioCount)")
}
}
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)queryMediaCount {
id<AIBudsDeviceInfoAPI> device = (id<AIBudsDeviceInfoAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceInfoAPI)]) {
NSLog(@"Device does not support media-count queries");
return;
}
[device requestQueryMediaCountInfoWithCompletion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Media-count query failed: %@", error.localizedDescription ?: @"Unknown error");
return;
}
AIBudsMediaCountInfoModel *counts = device.mediaCountInfo;
if (!counts) {
NSLog(@"The device did not provide media-count information");
return;
}
NSLog(@"Photos: %@", counts.photoCount);
NSLog(@"Videos: %@", counts.videoCount);
NSLog(@"Audio files: %@", counts.audioCount);
}];
}
@end错误处理
- 读取刷新后的属性前,先检查
success。 - 请求失败时,通过
error获取失败详情。 - 即使回调成功,也要处理返回
nil的mediaCountInfo。 - 除非目标设备文档明确说明,否则不要假设固定错误码。
最佳实践
- 成功后读取:仅在查询成功完成后访问
mediaCountInfo。 - 使用公开字段:分别读取照片、视频和音频数量。
- 检查协议支持:确认设备支持
DeviceInfoAPI。 - 在主队列更新 UI:将完成回调触发的 UIKit 更新派发到主队列。
注意事项
- 完成回调仅报告请求状态,不包含
MediaCountInfoModel结果。 MediaCountInfoModel以NSNumber值提供照片、视频和音频数量。- SDK 未在该模型中提供
totalCount属性。 - 拍摄、导入或删除文件时,媒体数量可能发生变化。