Tom Harrington
Tom Harrington

Reputation: 70956

MPNowPlayingInfoCenter Disappears when audio stream stalls

My app plays streaming audio via AVPlayer, and uses MPNowPlayingInfoCenter to display info about the stream on the device lock screen.

This works fine when audio is actually playing, but if the stream stalls due to network slowdowns (i.e. I receive AVPlayerItemPlaybackStalledNotification) the information disappears from the lock screen. But then if the stream resumes playing, it reappears.

This is confusing because when the now-playing info disappears from the lock screen it gives the appearance that the app has stopped playback. But then it resumes playback, when the lock screen UI seems to indicate that this won't happen.

Is there something I can do to make sure the now playing info remains visible whenever the stream should be playing but currently is not due to network speed issues? It seems like the only way to keep a consistent lock screen UI is to actually kill the network connection when it stalls, which is kind of stupid but at least not confusing.

In case more detail would help:

Upvotes: 31

Views: 1638

Answers (2)

Bhavesh Lathigara
Bhavesh Lathigara

Reputation: 1415

I am not getting any issue when set the lock screen even in network problem.

I am also dealing with streaming.

And I think lock screen only affected when audio session is active or not.

Here you can see my code and I am not getting any issue hope this will help to you.

-(void)setLockScreen
{
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    if (playingInfoCenter)
    {
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        NSError *myErr;
        if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr])
        {
            // Handle the error here.
            NSLog(@"Audio Session error %@, %@", myErr, [myErr userInfo]);
        }
        else
        {
            [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
            [self becomeFirstResponder];
        }
        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:imgViewLogo.image];
        NSArray *keys = [NSArray arrayWithObjects:
                         MPMediaItemPropertyTitle,
                         MPMediaItemPropertyArtist,
                         MPMediaItemPropertyArtwork,
                         MPNowPlayingInfoPropertyPlaybackRate,
                         nil];
        NSArray *values = [NSArray arrayWithObjects:
                           [[self.arrChannel objectAtIndex:[AppDelegate sharedAppDelegate].selectedRow] objectForKey:@"name"],
                           [[AppDelegate sharedAppDelegate].dictChannelsConfig objectForKey:@"venueName"],
                           albumArt,
                           [NSNumber numberWithInt:1],
                           nil];
        NSDictionary *mediaInfo = [NSDictionary dictionaryWithObjects:values forKeys:keys];
        keys = nil;
        values = nil;
        albumArt = nil;
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];
        mediaInfo = nil;
    }

}

Upvotes: 1

Marco Monteiro
Marco Monteiro

Reputation: 84

If I were to take a guess (and it's been a while since I've used AVFoundation) I would assume your audio session is being deactivated by the OS as soon as data stop flowing through the audio buffer. One trick would be to maintain a second AVPlayer which plays back silence to fill in the dead spots until you've buffered enough data to resume playback or reached some timeout and just give up. Use the notification to switch between player objects.

Upvotes: 1

Related Questions