メディア件数の取得
接続中のデバイスへ写真、動画、音声の最新件数を要求し、更新された 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プロパティを公開していません。 - ファイルの撮影、取り込み、削除中は、メディア件数が変化する場合があります。