简体   繁体   English

MPMusicPlayerController停止发送通知

[英]MPMusicPlayerController stops sending notifications

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 我有一个MPMusicPlayerController可以播放整个iPod库,并且我已订阅曲目更改等时的通知。这一切都正常工作

When the end of the playlist is reached, MPMusicPlayerController sends a change of state notification and stops. 到达播放列表的末尾时,MPMusicPlayerController发送状态更改通知并停止。 When I re-start the player, music begins to play again but MPMusicPlayerController no longer sends notifications when tracks change, etc. 当我重新启动播放器时,音乐会再次开始播放,但是当曲目改变时,MPMusicPlayerController不再发送通知。

Thoughts? 有什么想法吗?

Apparently, MPMusicPlayerControllerPlaybackStateDidChangeNotification is sometimes posted before the player object has actually updated its state. 显然,有时会在播放器对象实际更新其状态之前发布MPMusicPlayerControllerPlaybackStateDidChangeNotification。 However, you can still get the new state from the notification's userInfo dictionary (it's probably in there precisely for this reason). 但是,您仍然可以从通知的userInfo字典中获取新状态(正是由于这个原因,它可能在其中)。

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
}

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. 当播放器到达队列末尾时停止播放时,并没有“完全”停止播放,我最好的猜测是,当播放器停止播放时,它没有正确重置队列状态或类似的状态,因为我发现如果我在收到停止通知后发送了“停止”消息,则能够发送“播放”消息并使播放器正常重启。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM