我正在编写一个音乐播放应用程序,在每首歌之间执行一项任务。我需要这样做,即使当应用程序在后台,所以我需要知道什么时候一首歌已经完成。目前,我正在使用AVPlayer,它发送通知,即使应用程序在后台,但无法播放来自用户iCloud的歌曲。MPMediaPlayerController可以播放iCloud歌曲,但在应用程序处于后台时不发送通知(这对我的应用程序至关重要)。
所以,有没有人知道
AVPlayer播放iCloud歌曲的任何聪明方法,或者MPMusicPlayerController播放的歌曲何时完成?发布于 2016-01-28 21:09:53
你试过使用NSNotification中心和这两个观察者中的一个吗?MPMusicPlayerControllerPlaybackStateDidChangeNotification或MPMusicPlayerControllerNowPlayingItemDidChangeNotification。
此外,您还需要在您的MPMusicPlayerController.applicationMusicPlayer()或MPMusicPlayerController.systemMusicPlayer()实例上使用MPMusicPlayerController.systemMusicPlayer()。
发布于 2016-04-02 19:32:24
以金的回答为基础。您可以使用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属性在不同的应用程序运行时阶段调用各种方法。
希望这能帮上忙,如果你还有什么问题请告诉我。
https://stackoverflow.com/questions/33042749
复制相似问题