簡體   English   中英

iOS AVPlayer訴MPMusicPlayerController

[英]iOS AVPlayer v. MPMusicPlayerController

我正在編寫一個音樂播放應用程序,該程序在每首歌曲之間執行一項任務。 即使應用程序在后台,我也需要這樣做,所以我需要知道歌曲何時完成。 目前,我正在使用AVPlayer ,即使應用程序在后台,它也可以發送通知,但無法播放用戶iCloud上的歌曲。 MPMediaPlayerController可以播放iCloud歌曲,但在后台運行時不發送通知(這對於我的應用程序至關重要)。

所以,有人知道嗎

  1. AVPlayer播放iCloud歌曲的任何巧妙方法,或者
  2. MPMusicPlayerController播放的歌曲時,讓我的應用識別嗎?

您是否嘗試過使用NSNotification Center和這兩個觀察者之一? MPMusicPlayerControllerPlaybackStateDidChangeNotificationMPMusicPlayerControllerNowPlayingItemDidChangeNotification

此外,您還需要使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM