addObserver forkeypath在MPMusicPlayerController中不起作用

时间:2014-03-24 12:28:22

标签: ios mpmusicplayercontroller

我使用以下代码来了解 currentplayback 时间。但是当我使用MPMusicPlayerController播放歌曲时它无法正常工作。请帮助我。任何想法或建议都可能非常感激

[self addObserver:self forKeyPath:@"musicPlayer.currentPlaybackTime" options:0 context:nil];

歌曲播放代码

MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObject:song]];
[musicPlayer setQueueWithItemCollection:collection];

[musicPlayer prepareToPlay];
[musicPlayer setNowPlayingItem:song];
//[self.musicPlayer setVolume:volumeSlider.value];
[musicPlayer play];

1 个答案:

答案 0 :(得分:2)

如Volker所述,该财产不符合KVO标准。请参阅此处的文档以了解要监听的nsnotification事件。还要确保你打电话

[musicplayer beginGeneratingPlaybackNotifications]

[musicplayer endGeneratingPlaybackNotifications]

作为参考,这些是mpmusicplayercontroller发布的通知:

MPMusicPlayerControllerPlaybackStateDidChangeNotification
MPMusicPlayerControllerNowPlayingItemDidChangeNotification
MPMusicPlayerControllerPlaybackStateDidChangeNotification
MPMusicPlayerControllerNowPlayingItemDidChangeNotification

https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMusicPlayerController_ClassReference/Chapters/Reference.html#//apple_ref/occ/instm/MPMusicPlayerController/beginGeneratingPlaybackNotifications