【问题标题】:iOS: MPMusicPlayerControllerPlaybackStateDidChangeNotification called multiple times on certain devicesiOS:在某些设备上多次调用 MPMusicPlayerControllerPlaybackStateDidChangeNotification
【发布时间】:2012-07-07 01:09:33
【问题描述】:

我有一个播放音乐的应用程序。

我正在使用以下代码从 MPMusicPlayerController 监听播放状态更改以更新 UI。更准确地说,我在播放和暂停之间切换播放按钮的外观。

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter addObserver: self
                       selector: @selector (handle_NowPlayingItemChanged:)
                           name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                         object: self.musicPlayer];

[notificationCenter addObserver: self
                       selector: @selector (handle_PlaybackStateChanged:)
                           name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                         object: self.musicPlayer];

[self.musicPlayer beginGeneratingPlaybackNotifications];

这适用于 iPod Touch (iOS 5)iPhone 3GS (iOS 5)。每次播放状态发生变化时,我都会收到以下回调:

[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1

其中 1 表示MPMusicPlaybackStatePlaying

但是,如果我在 iPad 1 (iOS 5)iPad 2 (iOS 5)iPad 3 (iOS 6) 上运行相同的程序 strong> 我得到以下序列,而不是一个回调:

-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2

其中 2 表示 MPMusicPlaybackStatePaused 并导致我的应用程序在 UI 中显示错误的状态,因为实际上正在播放歌曲。

有趣的是,偶尔的顺序是

-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 2
-[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1

正确地以 1 MPMusicPlaybackStatePlaying 结束,但是回调被调用 5 次并具有交替值仍然没有意义。

关于如何解决这个问题的任何想法或建议我还可以测试什么来缩小问题范围?


由于到目前为止我还没有在这里收到答案,所以我也将问题交叉发布到 Apple 开发者论坛https://devforums.apple.com/thread/158426

【问题讨论】:

  • 我遇到了完全相同的问题。你找到解决方案了吗?

标签: ios ipad mpmediaplayercontroller


【解决方案1】:

我认为这与此处报告的错误相同:

Getting wrong playback state in MP Music Player Controller in ios 5

我针对该问题中的错误发布了解决方法。

【讨论】:

  • 我应该如何处理这些信息?我怎样才能得到正确的状态?或者如何重置播放器?
【解决方案2】:

您可以使用 currentPlaybackRate 属性检查实际播放状态。 MPMusicPlaybackStatePaused 必须匹配速率 0.0。下面显示了一个如何实现的示例...

- (void)musicPlayerControllerPlaybackStateDidChangeNotification:(NSNotification *)notification {
    float playbackRate = [((MPMusicPlayerController *)notification.object) currentPlaybackRate];
    MPMusicPlaybackState playbackState = (MPMusicPlaybackState)[notification.userInfo[@"MPMusicPlayerControllerPlaybackStateKey"] integerValue];
    switch (playbackState) {
        case MPMusicPlaybackStatePaused:
            if (playbackRate <= .0f) {
                self.playbackState = playbackState;
            }
            break;
        default:
            self.playbackState = playbackState;
            break;
    }
}

因此可以切断虚假的暂停通知。

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2019-10-25
    相关资源
    最近更新 更多