iOS: Updating Media Information in the Background

有些话、适合烂在心里 提交于 2019-12-19 04:56:27

问题


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


回答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



来源:https://stackoverflow.com/questions/13535397/ios-updating-media-information-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!