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

Viv*_*ikh 7 iphone objective-c ios4 ios ios5

我在MP音乐播放器中出现错误的播放状态.在播放一首歌时,我正处于暂停状态.我的应用程序在ios 4中工作正常.但我在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];
 }
Run Code Online (Sandbox Code Playgroud)

小智 0

我有同样的问题,但是当我的应用程序在后台时多次播放/暂停时,我向 Apple 报告了该错误,并希望很快得到答复,我想知道我的编码是否错误或存在 API 问题。如果这是您遇到的错误,这可能会有所帮助。我找到了一个解决方法,尽管这不是最好的解决方案,但对于我的应用程序来说是可以接受的:

在 viewDidLoad 中添加以下内容:

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

然后,创建一个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];
    }
}
Run Code Online (Sandbox Code Playgroud)

(不要将此代码放入您的playbackStateDidChanged方法中,因为这可能会产生无限循环)

这会将按钮和音乐播放器的状态同步到 API 报告的状态。在巧合的情况下,不会产生任何类型的影响,在其他情况下,播放器将相应地暂停/播放。