iOS app如何在进入背景模式的时候依然播放音乐
就像douban电台那样, 不但在退出程序以后依然有音乐播放, 就连双击home按钮以后左边的音乐控制栏都变成douban电台的图标了, 请问这是如何做到的?
PHP中文网2017-04-17 11:13:41
1. Background playback is done by adding Info.plist
to UIBackgroundModes
in audio
for reference https://developer.apple.com/library/i...
2. "Even after double-clicking the home button, the music control bar on the left turns into the icon of Douban Radio." This is to register an object in your own program as a responder to the corresponding remote control message. When the user clicks prev, next, play/pause in the control bar, the program will receive the corresponding message. When receiving the message, you must control the playback accordingly.
Register the responder in the view controller:
- (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; } - (void) viewWillDisappear:(BOOL)animated { [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; [super viewWillDisappear:animated]; } - (BOOL) canBecomeFirstResponder { return YES; }
At the same time, the view controller needs to implement:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent;
3. Making the control bar display a title is another matter. You need to call the media center interface in the program to set the title and cover. Referencehttp://stackoverflow.com/questions/10...
4. Getting the album cover and title from ID3 is the fourth thing. .