我正在做这个测试应用程序,我想在 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);
}
}