[英]How can I receive location updates while my application is in the background?
[英]How to receive MPMusicPlayerControllerNowPlayingItemDidChangeNotification while in background?
我正在做这个测试应用程序,当iPod更改正在播放的项目(歌曲)时,我想在其中接收通知,当应用程序处于前台时,测试运行良好,但是一旦应用程序进入后台,它就会停止接收通知是的,当我再次点击该应用程序(进入前台)时,我会根据该应用程序在后台运行时正在播放更改的所有时间获得所有通知,但是每次获取相同的歌曲信息时,如何获取每个通知的正确歌曲信息?
这是我在AppDelegate中所做的测试:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
[notificationCenter addObserver:self
selector:@selector(nowPlayingItemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:player];
[player beginGeneratingPlaybackNotifications];
return YES;
}
-(void) nowPlayingItemChanged:(NSNotification *)notification {
MPMusicPlayerController *player = (MPMusicPlayerController *)notification.object;
MPMediaItem *song = [player nowPlayingItem];
if (song) {
NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
NSString *album = [song valueForProperty:MPMediaItemPropertyAlbumTitle];
NSString *artist = [song valueForProperty:MPMediaItemPropertyArtist];
NSString *playCount = [song valueForProperty:MPMediaItemPropertyPlayCount];
NSLog(@"title: %@", title);
NSLog(@"album: %@", album);
NSLog(@"artist: %@", artist);
NSLog(@"playCount: %@", playCount);
}
}
进入后台时,请确保删除观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:musicPlayer];[player endGeneratingPlaybackNotifications];
进入前景时再次添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.