[英]iOS AVPlayer v. MPMusicPlayerController
我正在編寫一個音樂播放應用程序,該程序在每首歌曲之間執行一項任務。 即使應用程序在后台,我也需要這樣做,所以我需要知道歌曲何時完成。 目前,我正在使用AVPlayer
,即使應用程序在后台,它也可以發送通知,但無法播放用戶iCloud上的歌曲。 MPMediaPlayerController
可以播放iCloud歌曲,但在后台運行時不發送通知(這對於我的應用程序至關重要)。
所以,有人知道嗎
AVPlayer
播放iCloud歌曲的任何巧妙方法,或者 MPMusicPlayerController
播放的歌曲時,讓我的應用識別嗎? 您是否嘗試過使用NSNotification Center和這兩個觀察者之一? MPMusicPlayerControllerPlaybackStateDidChangeNotification
或MPMusicPlayerControllerNowPlayingItemDidChangeNotification
。
此外,您還需要使用beginGeneratingPlaybackNotifications()
在您的實例MPMusicPlayerController.applicationMusicPlayer()
或MPMusicPlayerController.systemMusicPlayer()
以@Kim的答案為基礎。 您可以使用NSNotificationCenter
並添加ObserverEvents
。 我為我的應用程序使用MPMusicPlayerController
類,並且已經注冊了該應用程序以使用NSNotificationCenter
屬性,因此我可以在不同的事件期間調用某些方法。
例如,在使用SystemMusicPlayer
屬性時,如果退出並終止應用程序進程,則音樂將繼續播放。 如果用戶要在退出時停止播放音樂,可以致電:
-(void) registerMediaPlayerNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(stopMusicWhenApplicationQuits)
name:UIApplicationWillTerminateNotification
object:[UIApplication sharedApplication]];
[musicPlayer beginGeneratingPlaybackNotifications];
}
您在其中看到@selector
的方法,當應用程序收到UIApplicationWillTerminateNotification
事件時將觸發該方法。 您可以在musicPlayer
中說什么
[self.musicPlayer stop];
因此,對於您遇到的問題,您仍然可以使用MediaPlayer
框架,使用MPMusicPlayerController
類,並使用NSNotificationCenter
屬性在不同的應用程序運行時階段調用各種方法。
希望對您有所幫助,如有其他問題,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.