简体   繁体   中英

iOS: Keep playing music in background with Spotify SDK

I'm using the Spotify iOS SDK and have a question about the background music.

When a user locks the phone or presses the Home button I want my app to continue to play the music. How is this possible?

I've tested

NSError *setCategoryErr = nil;

NSError *activationErr = nil;

 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; [[AVAudioSession sharedInstance] setActive:YES error:&activationErr]; 

Together with adding "Required background modes" to the apps plist.

Does this require that the AVAudioPlayer is used? Since I'm using the Spotify streamingPlayer its not possible.

Background playback should work just fine. You do need the audio item in your UIBackgroundModes Info.plist entry, though. Also, make sure you test on a device - I know remote control events don't work in the iOS Simulator, and that might be the case for background audio as well.

The iOS SDK will automatically set up the AVAudioSession for you, so you don't need to do that.

You might find this answer helpful, which is more in-depth: Background Audio with cocoalibspotify .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM