0

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).

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • 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. You'll need to add to your info.plist 'App plays audio' to 'Background Modes', and create the appropriate audio session. – sooper Nov 23 '12 at 20:39
  • @sooper So, if I setup a method to be called after, say, 5 minutes, it will still run, even if the application is in the background? I've found that processes stop and won't call until the application comes back to the foreground. Is that different when I have the 'Background Modes' setup for audio? I want to call a function completely separate to the `AVPlayer`. – RileyE Nov 23 '12 at 20:56
  • As long as `AVPlayer` is playing music, you will be able to run other things (timers, update now playing info etc..). – sooper Nov 23 '12 at 21:03
  • @sooper Well, I'm sorry for not having tried it beforehand. I thought it was just a given that processes wouldn't run if the app was in the background. I will give it a go! – RileyE Nov 23 '12 at 21:04
  • Check my answer, just to give you an idea of where to start. – sooper Nov 23 '12 at 21:10
  • 1
    Apple will now reject you if you run music in the background for the sake of keeping it alive. – Designerd Dec 23 '12 at 06:13

1 Answers1

1

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

sooper
  • 5,991
  • 6
  • 40
  • 65
  • Oh! I definitely didn't call `endReceivingRemoteControlEvents` in my dealloc. I thought it was implicitly called on dealloc. Good to know. What is `setActive:error:` doing? Its the only part that I'm missing, besides the dealloc call. – RileyE Nov 23 '12 at 21:14
  • I just re-read the docs and I figured out that its to override other applications' audio, if there is a conflict. Thanks! I'm going to leave this unchecked for now, until I verify that it works. – RileyE Nov 23 '12 at 21:15