iOS: MPMusicPlayerControllerPlaybackStateDidChangeNotification called multiple times on certain devices

℡╲_俬逩灬. 提交于 2020-01-13 12:08:12

问题


I have an application that plays back music.

I'm using the following code to listen to playback state changes from the MPMusicPlayerController to update the UI. More precisely I toggle the look of the play button between play and pause.

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];

This works great on an iPod Touch (iOS 5) and iPhone 3GS (iOS 5). Every time the playback state changes I get the following callback:

[JBMediaPlayer handle_PlaybackStateChanged:] :: playbackState: 1

where 1 means MPMusicPlaybackStatePlaying.

However if I run the same on a iPad 1 (iOS 5), iPad 2 (iOS 5) or iPad 3 (iOS 6) I get the following sequence instead of just one single callback:

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

where 2 means MPMusicPlaybackStatePaused and causes my application to display the wrong state in the UI, because the song is actually being played back.

The funny thing is, that once in a while the sequence is

-[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

which ends up correctly with 1 MPMusicPlaybackStatePlaying, however still doesn't make sense that the callback gets called 5 times, with alternating values.

Any ideas on how to solve this or suggestion what else I can test to narrow down the problem?


Since I haven't received an answer here so far, I also cross-posted the question to the Apple Developer Forum: https://devforums.apple.com/thread/158426


回答1:


I think this is the same bug reported here:

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

I posted a workaround for the bug in that question.




回答2:


You can check real playback state using currentPlaybackRate property. MPMusicPlaybackStatePaused must match rate 0.0. An example how can it implemented is shown below...

- (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;
    }
}

Thus it is possible to cut off false pause notification.



来源:https://stackoverflow.com/questions/11364503/ios-mpmusicplayercontrollerplaybackstatedidchangenotification-called-multiple-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!