在ios 5 MP音乐播放器控制器中出现错误的播放状态

MP音乐播放器中出现错误的播放状态。 在播放歌曲时我正在暂停状态。 我的应用程序工作正常在ios 4.but但我有这个问题在iOS 5中。任何人都可以帮助我?

我的代码在这里。

[musicPlayer stop]; if (userMediaItemCollection) { userMediaItemCollection=nil; } musicPlayer.nowPlayingItem=nil; userMediaItemCollection=[MPMediaItemCollection collectionWithItems:[mediaItemCollection items]]; [musicPlayer setQueueWithItemCollection:userMediaItemCollection]; [musicPlayer setNowPlayingItem: [[userMediaItemCollectionitems]objectAtIndex:indexOfCurrentObject]]; [self enablePrevAndNextButtons]; [musicPlayer play]; } -(void)playbackStateDidChanged:(NSNotification *)notification { if (musicPlayer.playbackState!=MPMusicPlaybackStatePlaying) { [playPauseButton setBackgroundImage:[UIImage imageNamed:@"play_iPad.png"] forState:UIControlStateNormal]; } else if(musicPlayer.playbackState==MPMusicPlaybackStatePlaying) { [playPauseButton setBackgroundImage:[UIImage imageNamed:@"pause_iPad.png"] forState:UIControlStateNormal]; } 

我也向苹果报告了这个错误。 通过执行以下操作,我能够100%地重现它:

启动使用MPMusicPlayerController的应用程序。 启动“音乐”应用程序。 点击播放,跳过,跳过,暂停,播放,暂停打开原始应用程序,并且MPMusicPlayerController的MPMusicPlaybackState将不正确。

这里提出的解决scheme都没有为我工作。 确实有效的解决scheme是跟踪错误发生的时间,并在这些情况下更新UI。

当接收到UIApplicationDidBecomeActiveNotification通知时(请参阅matbur文章以获取更多详细信息),请查看MPMusicPlaybackState所说的是否实际上不播放audio:

 -(BOOL) isPlaybackStateBugActive { MPMusicPlaybackState playbackState = self.musicPlayer.playbackState; if (playbackState == MPMusicPlaybackStatePlaying) { AudioSessionInitialize (NULL, NULL, NULL, NULL); UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory); AudioSessionSetActive (true); UInt32 audioIsPlaying; UInt32 size = sizeof(audioIsPlaying); AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &size, &audioIsPlaying); if (!audioIsPlaying){ NSLog(@"PlaybackState bug is active"); return YES; } } return NO; } 

不要忘记导入AudioToolbox框架。

这些解决方法都没有解决我的应用程序的问题。 这是iOS中的一个错误,我的应用程序将永远不能正常运行,直到苹果修复它。

我有一个播放/暂停button的音乐播放器。 在播放音乐时,button显示“暂停”图标。 音乐暂停时,button显示“播放”图标 – 就像所有的音乐应用程序一样。 我可以随时复制这个错误:1.在我的应用程序中播放音乐(播放/暂停button正确显示“暂停”图标)2.背景我的应用程序,并locking我的手机约10分钟3.双轻点家中,并从iPod控制按暂停button4.解锁我的手机,并再次打开我的应用程序5.音乐将停止,但我的应用程序仍然显示“暂停”图标,当它应该这样“播放”

我已经做了大量的debugging和日志logging,以确保更新我的播放/暂停button的方法总是在应用程序激活时调用。 问题是,当我重新进入我的应用程序时,即使音乐停止/暂停,MPMusicPlayer的播放状态仍然被设置为MPMusicPlaybackStatePlaying。

大约一年前我提交了一个bug报告,而且还没有听到苹果的任何消息。 如果其他人将提交一个它将不胜感激。

我有同样的问题,但是当我的应用程序在后台播放/暂停很多次时,我向苹果公司报告了这个错误,并希望尽快得到一个更好的答案,我想知道我的编码是错误还是API问题。 如果这是您遇到的错误,这可能会有所帮助。 我来到一个解决方法,即使不是最好的解决scheme,因为我的应用程序是可以接受的:

在viewDidLoad中添加这个:

 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter addObserver: self selector: @selector (handle_ApplicationDidBecomeActive:) name: UIApplicationDidBecomeActiveNotification object: nil]; 

然后,创build一个handle_ApplicationDidBecomeActive方法,并添加这个:

 - (void) handle_ApplicationDidBecomeActive: (id) notification { if (musicPlayer.playbackState!=MPMusicPlaybackStatePlaying) { [playPauseButton setBackgroundImage:[UIImage imageNamed:@"play_iPad.png"] forState:UIControlStateNormal]; [musicPlayer pause]; } else if(musicPlayer.playbackState==MPMusicPlaybackStatePlaying) { [playPauseButton setBackgroundImage:[UIImage imageNamed:@"pause_iPad.png"] forState:UIControlStateNormal]; [musicPlayer pause]; } } 

(不要把这个代码放到你的playbackStateDidChanged方法中,因为这可能会产生一个无限循环)

这会将您的button和音乐播放器的状态同步到API报告的状态。 在有巧合的情况下,不会有任何types的影响,在其他情况下,玩家将相应地暂停/播放。

我遇到了同样的问题与iOS 5的发布。我发现playbackState属性确实得到更新,但延迟之后,所以它还没有设置时,您的playbackStateDidChanged方法运行。

我的解决方法是设置我自己的实例variables,称为musicPlayerRecentlyStartedPlaying,每当我开始播放。 然后我使用从计时器调用的方法来检查该variables和playbackState属性,以确定玩家是否真的在玩游戏:

 - (void)playMusic { [self.musicPlayer play]; self.musicPlayerRecentlyStartedPlaying = TRUE; self.musicControlsUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateMusicControls:) userInfo:nil repeats:TRUE]; } - (void)stopMusic { [self.musicPlayer stop]; self.musicPlayerRecentlyStartedPlaying = FALSE; [self.musicControlsUpdateTimer invalidate]; } - (void)updateMusicControls:(NSTimer *)timer { BOOL playing = (([self.musicPlayer playbackState] == MPMusicPlaybackStatePlaying)&&(self.musicPlayer.nowPlayingItem)); if (!playing) { // check to see if we recently started playing if (self.musicPlayerRecentlyStartedPlaying) { playing = TRUE; } } else { // once the property is updated, we no longer need this self.musicPlayerRecentlyStartedPlaying = FALSE; } } 

您可能不需要从计时器中调用updateMusicControls,但是我也是这样做的,因为我还在音乐播放时更新进度条的位置。

此代码使用当前的button点击上一首歌曲将被播放

 - (IBAction)playPreviousSongInList:(id)sender { static NSTimeInterval skipToBeginningOfSongIfElapsedTimeLongerThan = 3.5; NSTimeInterval playbackTime = self.musicPlayer.currentPlaybackTime; if (playbackTime <= skipToBeginningOfSongIfElapsedTimeLongerThan) { [self.musicPlayer skipToPreviousItem]; } else { [self.musicPlayer skipToBeginning]; } }