मुख्य कंटेंट तक स्किप करें

मीडिया फ़ाइलों की संख्या पूछें

जुड़े डिवाइस से photo, video और audio की नवीनतम संख्या माँगें, फिर अपडेट हुई mediaCountInfo property पढ़ें।

पहले की आवश्यकताएँ

Media counts पूछने से पहले सुनिश्चित करें:

  • डिवाइस connected और stable state में है
  • डिवाइस DeviceInfoAPI protocol का समर्थन करता है

API संदर्भ

फ्रेमवर्क

AIBuds.xcframework

Import

SDK उपयोग करने वाली files में main framework import करें:

Swift
import AIBuds

प्रोटोकॉल

Request method और result property 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

डिवाइस में रखी media files की नवीनतम संख्या माँगता है।

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

पैरामीटर

पैरामीटरप्रकारविवरण
completionAIBudsCompletionHandler?Request पूरा होने पर call होने वाला optional completion handler।

Callback पैरामीटर:

नामप्रकारविवरण
successBool / BOOLRequest सफल हो तो true, अन्यथा false
errorNSError?Request असफल हो तो error details, अन्यथा nil

Result properties:

Propertyप्रकारविवरण
photoCountNSNumberPhotos की संख्या।
videoCountNSNumberVideos की संख्या।
audioCountNSNumberAudio files की संख्या।

Return value

Method completion handler में media counts return नहीं करता। सफल request के बाद अपडेट हुई mediaCountInfo property पढ़ें।

उपयोग के उदाहरण

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. Refresh हुई property पढ़ने से पहले success जाँचें।
  2. Request असफल होने पर details के लिए error उपयोग करें।
  3. सफल callback के बाद भी nil मिलने की स्थिति संभालें, क्योंकि mediaCountInfo उपलब्ध न हो सकता है।
  4. लक्ष्य डिवाइस के लिए documented न होने वाले fixed error codes न मानें।

बेहतर तरीके

  1. सफलता के बाद पढ़ें: Query सफल होने के बाद ही mediaCountInfo पढ़ें।
  2. Published fields उपयोग करें: Photo, video और audio counts अलग-अलग पढ़ें।
  3. Protocol conformance जाँचें: DeviceInfoAPI support की पुष्टि करें।
  4. UI main queue पर अपडेट करें: Completion से आने वाले UIKit updates main queue पर भेजें।

ध्यान देने योग्य बातें

  • Completion handler request status देता है; उसमें MediaCountInfoModel result नहीं होता।
  • MediaCountInfoModel photo, video और audio counts को NSNumber values के रूप में देता है।
  • SDK इस model में totalCount property नहीं देता।
  • Files capture, import या delete होने के दौरान media counts बदल सकते हैं।