iOS:在某些设备上多次调用MPMusicPlayerControllerPlaybackStateDidChangeNotification [英] iOS: MPMusicPlayerControllerPlaybackStateDidChangeNotification called multiple times on certain devices
问题描述
我有一个播放音乐的应用程序。
I have an application that plays back music.
我正在使用以下代码从MPMusicPlayerController监听播放状态更改以更新UI。更确切地说,我在播放和暂停之间切换播放按钮的外观。
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];
这适用于 iPod Touch(iOS 5)和 iPhone 3GS(iOS 5)。每次回放状态改变时,我得到以下回调:
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
其中1表示 MPMusicPlaybackStatePlaying
。
但是,如果我在 iPad 1(iOS 5), iPad 2(iOS 5)或 iPad 3( iOS 6)我得到以下序列而不是只有一个回调:
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
其中2表示 MPMusicPlaybackStatePaused
并导致我的申请在UI中显示错误的状态,因为该歌曲实际上正在播放。
where 2 means MPMusicPlaybackStatePaused
and causes my application to display the wrong state in the UI, because the song is actually being played back.
有趣的hing是,偶尔序列是
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
以1 MPMusicPlaybackStatePlaying正确结束
,但仍然没有意义,回调被调用5次,交替值。
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?
由于到目前为止我还没有收到答案,我也是将问题交叉发布到 Apple开发人员论坛: https://devforums.apple .COM / thread / 158426
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
推荐答案
您可以使用currentPlaybackRate属性检查实际播放状态。 MPMusicPlaybackStatePaused必须与速率0.0匹配。它如何实现的示例如下所示...
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.
这篇关于iOS:在某些设备上多次调用MPMusicPlayerControllerPlaybackStateDidChangeNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!