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

म्यूज़िक प्लेबैक नियंत्रित करें

कनेक्टेड डिवाइस को प्लेबैक, track बदलने, mute और प्लेबैक वॉल्यूम के commands भेजें।

आवश्यकताएँ

  • डिवाइस कनेक्टेड और ready है।
  • डिवाइस DeviceMusicControlAPI के अनुरूप है।
  • दिए गए वॉल्यूम मान 0 से 100 के बीच हैं।

AI की सहायता से लागू करें

AI से बनाएँ

इस वर्कफ़्लो को AI से लागू करें

आधिकारिक “AIBuds संगीत नियंत्रित करें” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।

https://docs-aibuds.github.io/hi/skills/control-aibuds-music को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds संगीत नियंत्रित करें” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।
आधिकारिक स्किल देखें

API संदर्भ

Framework

AIBuds.xcframework

Import

Swift
import AIBuds

प्रोटोकॉल

Commands DeviceMusicControlAPI में परिभाषित हैं।

Swift
/// The protocol for device music control API.
protocol DeviceMusicControlAPI: DeviceAPI {
    /// Starts or resumes music playback.
    ///   - 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 playMusic(_ completion: AIBudsCompletionHandler?)

    /// Pauses the currently playing music.
    ///   - 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 pauseMusic(_ completion: AIBudsCompletionHandler?)

    /// Skips to the next track in the playback queue.
    ///   - 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 playNextMusic(_ completion: AIBudsCompletionHandler?)

    /// Returns to the previous track in the playback queue.
    ///   - 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 playPreviousMusic(_ completion: AIBudsCompletionHandler?)

    /// Increases the playback volume by one step.
    ///   - 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 musicVolumeUp(_ completion: AIBudsCompletionHandler?)

    /// Decreases the playback volume by one step.
    ///   - 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 musicVolumeDown(_ completion: AIBudsCompletionHandler?)

    /// Mutes the playback volume.
    ///   - 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 mute(_ completion: AIBudsCompletionHandler?)

    /// Unmutes the playback volume.
    ///   - 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 unmute(_ completion: AIBudsCompletionHandler?)

    /// Sets the playback volume to a specific level.
    /// - Parameters:
    ///   - volume: The desired 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 setMusicVolume(
        _ volume: Int,
        completion: AIBudsCompletionHandler?
    )
}

Instance विधियाँ

Methodउद्देश्य
playMusicप्लेबैक शुरू या resume करता है।
pauseMusicप्लेबैक pause करता है।
playNextMusicअगले track पर जाता है।
playPreviousMusicपिछले track पर जाता है।
musicVolumeUpप्लेबैक वॉल्यूम एक step बढ़ाता है।
musicVolumeDownप्लेबैक वॉल्यूम एक step घटाता है।
muteप्लेबैक mute करता है।
unmuteप्लेबैक unmute करता है।
setMusicVolumeप्लेबैक वॉल्यूम 0 से 100 तक सेट करता है।

पैरामीटर

ParameterTypeविवरण
volumeInt / NSInteger0 से 100 तक वांछित प्लेबैक वॉल्यूम।
completionAIBudsCompletionHandler?Optional completion handler।

Callback पैरामीटर:

नामTypeविवरण
successBool / BOOLCommand सफल हुआ या नहीं।
errorNSError?विफलता का विवरण; सफलता पर nil

Return मान

ये methods सीधे कोई मान return नहीं करते।

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

प्लेबैक

Swift
import AIBuds

guard let device = device as? DeviceMusicControlAPI else {
    print("Device does not support music control")
    return
}

device.playMusic { success, error in
    guard success else {
        print("Play failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Playback command succeeded")
}

प्लेबैक वॉल्यूम

Swift
device.setMusicVolume(60) { success, error in
    if !success {
        print(error?.localizedDescription ?? "Volume update failed")
    }
}

त्रुटि प्रबंधन

हर command के लिए success जाँचें और विफलता के विवरण के लिए error उपयोग करें। Call करने से पहले निश्चित volume values validate करें; public contract clamping की गारंटी नहीं देता।

श्रेष्ठ अभ्यास

  1. SDK के सही नाम उपयोग करें: Generic play, pause, nextTrack या previousTrack methods से न बदलें।
  2. निश्चित वॉल्यूम को Debounce करें: SDK Demo तेज़ी से commands भेजने से बचने के लिए slider writes में देरी करता है।
  3. Volume APIs अलग रखें: Playback controls के लिए यह protocol और typed device channels के लिए DeviceVolumeControlAPI उपयोग करें।
  4. सफलता के बाद UI अपडेट करें: केवल dispatch होने पर command को सफल न मानें।

नोट्स

  • Protocol में stop command या अलग support property नहीं है।
  • Track metadata और queue contents इस API के दायरे से बाहर हैं।