Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i am getting wrong playback state in MP music player. while playing a song i am getting pause state. My app is working fine in ios 4.but i am having this issue in ios 5. can anybody Help me ??

My code is here.

[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];
 }
like image 802
Vivek Parikh Avatar asked Apr 12 '12 06:04

Vivek Parikh


2 Answers

I have also reported this bug to Apple. I was able to reproduce it 100% of the time by doing the following:

Launch application that uses MPMusicPlayerController. Launch the "Music" App. Hit Play, Skip, Skip, Pause, Play, Pause Open the original application and the MPMusicPlaybackState of MPMusicPlayerController will be incorrect.

None of the proposed solutions here worked for me. The solution that did work was to keep track of when the bug was occurring and updating the UI specially in these cases.

When the UIApplicationDidBecomeActiveNotification notification is received (see matbur post for more details on this), see if audio is actually not playing when the MPMusicPlaybackState said it was:

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

Don't forget to import the AudioToolbox framework.

like image 177
pgkelley Avatar answered Oct 24 '22 07:10

pgkelley


None of these workarounds fix the issue for my app. It is a bug in iOS, and my app will never function properly until Apple fixes it.

I have a music player with a play/pause button. When music is playing, the button shows the "pause" icon. When music is paused, the button shows the "play" icon - just like all music apps. I can replicate the bug at any time by doing the following: 1. Play music in my app (the play/pause button shows the "pause" icon correctly) 2. Background my app and lock my phone for ~10 minutes 3. Double tap home and hit the pause button from the iPod controls 4. Unlock my phone and open my app again 5. Music will be stopped, but my app still shows the "pause" icon when it should so "play"

I've done extensive debugging and logging to ensure that the method that updates my play/pause button is always called when my application becomes active. The issue is that when I re-enter my app, the playback state of MPMusicPlayer is still set to MPMusicPlaybackStatePlaying even when music is stopped/paused.

I filed a bug report for this about a year ago and haven't heard anything from Apple. If someone else would file one it would be greatly appreciated.

like image 20
mms13 Avatar answered Oct 24 '22 06:10

mms13