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
DeviceMusicControlAPIprotokolü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.API Referansı
Framework
AIBuds.xcframework
İçe Aktarma
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>Protokol
Komutlar DeviceMusicControlAPI tarafından tanımlanır.
- Swift
- Objective-C
/// 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?
)
}/// The protocol for device music control API.
@protocol AIBudsDeviceMusicControlAPI <AIBudsDeviceAPI>
/// 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.
- (void)playMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)pauseMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)playNextMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)playPreviousMusicWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)musicVolumeUpWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)musicVolumeDownWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)muteWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)unmuteWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)setMusicVolume:(NSInteger)volume completion:(AIBudsCompletionHandler _Nullable)completion;
@endÖrnek Yöntemleri
| Yöntem | Amaç |
|---|---|
playMusic | Oynatmayı başlatır veya sürdürür. |
pauseMusic | Oynatmayı duraklatır. |
playNextMusic | Sonraki parçaya geçer. |
playPreviousMusic | Önceki parçaya geçer. |
musicVolumeUp | Oynatma sesini bir adım artırır. |
musicVolumeDown | Oynatma sesini bir adım azaltır. |
mute | Oynatma sesini kapatır. |
unmute | Oynatma sesini açar. |
setMusicVolume | Oynatma sesini 0-100 arasında ayarlar. |
Parametreler
| Parametre | Tür | Açıklama |
|---|---|---|
volume | Int / NSInteger | 0-100 arasında istenen oynatma ses düzeyi. |
completion | AIBudsCompletionHandler? | İsteğe bağlı completion handler. |
Callback Parametreleri:
| Ad | Tür | Açıklama |
|---|---|---|
success | Bool / BOOL | Komutun başarılı olup olmadığı. |
error | NSError? | 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
- Objective-C
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")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceMusicControlAPI> device = (id<AIBudsDeviceMusicControlAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceMusicControlAPI)]) {
[device playMusicWithCompletion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Play failed: %@", error.localizedDescription);
return;
}
NSLog(@"Playback command succeeded");
}];
}Oynatma Ses Düzeyi
- Swift
- Objective-C
device.setMusicVolume(60) { success, error in
if !success {
print(error?.localizedDescription ?? "Volume update failed")
}
}[device setMusicVolume:60
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"%@", error.localizedDescription);
}
}];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
- SDK Adlarını Aynen Kullanın: Genel
play,pause,nextTrackveyapreviousTrackyöntemlerini bunların yerine kullanmayın. - Ses Düzeyi Yazmalarını Seyreltin: SDK Demo'su hızlı komutları önlemek için kaydırıcı yazmalarını geciktirir.
- Ses API'lerini Ayırın: Oynatma denetimleri için bu protokolü, türü belirlenmiş cihaz kanalları için
DeviceVolumeControlAPIkullanın. - 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.