Ana içeriğe geç

Müzik Oynatmayı Denetleme

Bağlı cihaza oynatma, parça geçişi, sessize alma ve oynatma ses düzeyi komutları gönderin.

Ön Koşullar

  • Cihaz bağlı ve hazırdır.
  • Cihaz DeviceMusicControlAPI protokolüne uyar.
  • Doğrudan ses düzeyi değerleri 0-100 arasındadır.

AI desteğiyle uygulayın

AI ile geliştirin

Bu iş akışını AI ile uygulayın

Resmî “AIBuds Müzik Kontrolü” becerisini kullanarak iş akışını uygulamanıza uyarlayın.

https://docs-aibuds.github.io/tr/skills/control-aibuds-music adresini okuyup yönergeleri izleyin. Bu beceriyle “AIBuds Müzik Kontrolü” iş akışını iOS projesinde uygulayıp doğrulayın.
Resmî beceriyi görüntüle

API Referansı

Framework

AIBuds.xcframework

İçe Aktarma

Swift
import AIBuds

Protokol

Komutlar DeviceMusicControlAPI tarafından tanımlanır.

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

Örnek Yöntemleri

YöntemAmaç
playMusicOynatmayı başlatır veya sürdürür.
pauseMusicOynatmayı duraklatır.
playNextMusicSonraki parçaya geçer.
playPreviousMusicÖnceki parçaya geçer.
musicVolumeUpOynatma sesini bir adım artırır.
musicVolumeDownOynatma sesini bir adım azaltır.
muteOynatma sesini kapatır.
unmuteOynatma sesini açar.
setMusicVolumeOynatma sesini 0-100 arasında ayarlar.

Parametreler

ParametreTürAçıklama
volumeInt / NSInteger0-100 arasında istenen oynatma ses düzeyi.
completionAIBudsCompletionHandler?İsteğe bağlı completion handler.

Callback Parametreleri:

AdTürAçıklama
successBool / BOOLKomutun başarılı olup olmadığı.
errorNSError?Hata ayrıntıları; başarılıysa nil.

Dönüş Değeri

Bu yöntemler doğrudan değer döndürmez.

Kullanım Örnekleri

Oynatma

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

Oynatma Ses Düzeyi

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

Hata Yönetimi

Her komut için success değerini denetleyin ve hata ayrıntıları için error kullanın. Çağrıdan önce doğrudan ses değerini doğrulayın; genel sözleşme aralık dışı değerleri sınırlamayı garanti etmez.

En İyi Uygulamalar

  1. SDK Adlarını Aynen Kullanın: Genel play, pause, nextTrack veya previousTrack yöntemlerini bunların yerine kullanmayın.
  2. Ses Düzeyi Yazmalarını Seyreltin: SDK Demo'su hızlı komutları önlemek için kaydırıcı yazmalarını geciktirir.
  3. Ses API'lerini Ayırın: Oynatma denetimleri için bu protokolü, türü belirlenmiş cihaz kanalları için DeviceVolumeControlAPI kullanın.
  4. Arayüzü Başarıdan Sonra Güncelleyin: Komutun yalnızca gönderilmiş olmasını başarı kabul etmeyin.

Notlar

  • Protokol durdurma komutu veya ayrı destek özelliği içermez.
  • Parça meta verileri ve kuyruk içeriği bu API'nin kapsamı dışındadır.