Skip to main content

Query Media Count

Request the latest photo, video, and audio counts from a connected device, then read the updated mediaCountInfo property.

Prerequisites

Before querying media counts, ensure:

  • The device is connected and in a stable state
  • The device supports the DeviceInfoAPI protocol

API Reference

Framework

AIBuds.xcframework

Import

In the files where you want to use the SDK, import the main framework:

Swift
import AIBuds

Protocol

The request method and result property are defined in DeviceInfoAPI.

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

Instance Method

Requests the latest counts for media stored on the device.

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

Parameters

ParameterTypeDescription
completionAIBudsCompletionHandler?Optional completion handler called when the request finishes.

Callback Parameters:

NameTypeDescription
successBool / BOOLtrue if the request succeeded; otherwise false.
errorNSError?Error details if the request failed; otherwise nil.

Result Properties:

PropertyTypeDescription
photoCountNSNumberThe number of photos.
videoCountNSNumberThe number of videos.
audioCountNSNumberThe number of audio files.

Return Value

The method does not return media counts in its completion handler. After a successful request, read the updated mediaCountInfo property.

Usage Examples

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

Error Handling

  1. Check success before reading the refreshed property.
  2. Use error for failure details when the request does not succeed.
  3. Handle a nil mediaCountInfo value even after a successful callback.
  4. Do not assume fixed error codes unless they are documented for the target device.

Best Practices

  1. Read After Success: Access mediaCountInfo only after the query completes successfully.
  2. Use the Published Fields: Read photo, video, and audio counts independently.
  3. Check Protocol Conformance: Confirm that the device supports DeviceInfoAPI.
  4. Update UI on the Main Queue: Dispatch completion-driven UIKit updates to the main queue.

Notes

  • The completion handler reports request status; it does not contain a MediaCountInfoModel result.
  • MediaCountInfoModel exposes photo, video, and audio counts as NSNumber values.
  • The SDK does not expose a totalCount property in this model.
  • Media counts can change while files are captured, imported, or deleted.