實例要建立在@金正日的答案。您可以使用NSNotificationCenter
並添加ObserverEvents
。我爲我的應用程序使用了MPMusicPlayerController
類,並且我註冊了應用程序以使用NSNotificationCenter
屬性,因此我可以在不同事件期間調用某些方法。
例如,如果退出並終止應用程序過程,則使用SystemMusicPlayer
屬性,但音樂會繼續播放。如果用戶想阻止時,他們exityou可以稱之爲音樂:
-(void) registerMediaPlayerNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(stopMusicWhenApplicationQuits)
name:UIApplicationWillTerminateNotification
object:[UIApplication sharedApplication]];
[musicPlayer beginGeneratingPlaybackNotifications];
}
當你看到@selector
那就是當應用程序收到事件將被解僱的方法。這在musicPlayer
可以說
[self.musicPlayer stop];
所以你有問題,你仍然可以使用MediaPlayer
框架,使用MPMusicPlayerController
類和使用NSNotificationCenter
性能通話過程中不同的應用程序運行階段的各種方法。
希望這可以幫助,讓我知道如果你有任何進一步的問題。
你有沒有爲你的應用程序解決這個問題? – newenglander