问题
I have a MPMusicPlayerController playing the entire iPod library and I'm subscribed to the notifications when tracks change etc. This is all working correctly
When the end of the playlist is reached, MPMusicPlayerController sends a change of state notification and stops. When I re-start the player, music begins to play again but MPMusicPlayerController no longer sends notifications when tracks change, etc.
Thoughts?
回答1:
Apparently, MPMusicPlayerControllerPlaybackStateDidChangeNotification is sometimes posted before the player object has actually updated its state. However, you can still get the new state from the notification's userInfo dictionary (it's probably in there precisely for this reason).
In code:
- (void)playbackStateDidChange:(NSNotification *)notification {
static NSString * const stateKey = @"MPMusicPlayerControllerPlaybackStateKey";
NSNumber *number = [[notification userInfo] objectForKey:stateKey];
MPMusicPlaybackState state = [number integerValue];
// state is the new state
MPMusicPlayerController *player = [notification object];
// state may not be equal to player.playbackState
}
回答2:
After alot of experimenting, here's what resolved my issue.
As it turns out, the notification was being sent and the state was being reported as "stopped"; however sending a "play" message only resulted in another notification firing and the state still appearing as "stopped".
While the player was stopping when it reached the end of the queue, it wasn't "completely" stopped, my best guess is that while it stopped playing, it didn't properly reset the queue state or something like that, because I found that if I sent a "stop" message after the stop notification was received, I was able to send a "play" message and have the player restart properly.
来源:https://stackoverflow.com/questions/1324409/mpmusicplayercontroller-stops-sending-notifications