iOS:在某些设备上多次调用MPMusicPlayerControllerPlaybackStateDidChangeNotification

iOS:在某些设备上多次调用MPMusicPlayerControllerPlaybackStateDidChangeNotification

问题描述:

我有一个播放音乐的应用程序。iOS:在某些设备上多次调用MPMusicPlayerControllerPlaybackStateDidChangeNotification

我正在使用以下代码来监听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中)相同的得到以下序列,而不只是一个单一的回调:

-[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次,交替值。

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


因为我还没有收到答复到这里为止,我还交张贴问题向苹果开发者论坛https://devforums.apple.com/thread/158426

+0

我有完全相同的问题。你有没有找到解决方案? – 2012-10-12 10:21:49

我觉得这是此报告的同样的bug:

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

我发布了解决办法在这个问题的错误。

+0

我该怎么处理这些信息?我怎样才能得到正确的状态?或者我该如何重置播放器? – MetaImi 2012-11-27 23:51:30

您可以使用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; 
    } 
} 

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