31

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:

  • When the app gets that notification, the only thing it does is update the UI.
  • The app never clears MPNowPlayingInfoCenter when there's a current program, so as long as the stream is supposed to be playing, there's non-empty data that should be getting displayed.
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Are you setting the info center dictionary from a time observer? For example: `addPeriodicTimeObserverForInterval:` – yairsz May 06 '15 at 00:33
  • No, I'm not using a time observer. The now-playing info is only updated in direct response to user actions and when the app enters the foreground. – Tom Harrington May 06 '15 at 02:58
  • Are you listening on `AVPlayerItemPlaybackStalledNotification` ? What happens when you not listening? And what is about caching your data-streams? – Miralem Cebic May 06 '15 at 14:08
  • Who owns the dictionary, is it a strong property or a local variable? – yairsz May 06 '15 at 15:05
  • @MiralemCebic Yes I'm listening for that notification. If I don't listen for it then I can't update the UI, so it looks like it's still playing even though there's no sound. I don't change the now-playing info when that notification is received. – Tom Harrington May 06 '15 at 15:31
  • @yairsz The dictionary is a local, but since `MPNowPlayingInfoCenter` declares `nowPlayingInfo` to be a `copy` ivar, that shouldn't matter. Regardless it works normally except when an audio stall occurs. – Tom Harrington May 06 '15 at 15:34

2 Answers2

1

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.

1

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

}
Bhavesh Lathigara
  • 1,355
  • 1
  • 15
  • 25