Question

I'm currently using MPNowPlayingInfoCenter for my app to show which song is playing, but I am looking to incorporate HTTP Live Streaming into my application, which will have any number of different tracks occurring in the background.

Is there a way to set the nowPlayingInfo while the application is in the background (Call a function after a certain amount of time?), even though applications technically don't actually run in the background?

Or is there a way to set the now playing information using a simple call on my server that will return the proper information - Using an API call that returns a string or image?

I know that it is possible, since Songza has already done it, but maybe they have received permission to use certain private methods from Apple (if thats even a thing you can do).

Was it helpful?

Solution

If you're using the AVPlayer class and the primary purpose of your app is to play music, then you'll be able to run it in the background and thus update the nowPlayingInfo when the track is changed.

Just a quick example:

- (void)viewDidLoad {
    [super viewDidLoad]

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
        //These two steps are important if you want the user to be able to change tracks with remote controls (you'll have to handle the remote control events yourself).
    }
    self.yourPlayer = [[AVPlayer alloc] init];
}

Unregister the remote control events in your dealloc method:

[[UIApplication sharedApplication] endReceivingRemoteControlEvents]

Change Required Background Modes in your info.plist to App plays audio

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top