简体   繁体   English

iOS:使用Spotify SDK继续在后台播放音乐

[英]iOS: Keep playing music in background with Spotify SDK

I'm using the Spotify iOS SDK and have a question about the background music. 我正在使用Spotify iOS SDK,并且对背景音乐有疑问。

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 * setCategoryErr = nil;

NSError *activationErr = 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. 并在应用程序plist中添加“必需的背景模式”。

Does this require that the AVAudioPlayer is used? 这是否要求使用AVAudioPlayer? Since I'm using the Spotify streamingPlayer its not possible. 由于我正在使用Spotify StreamingPlayer,因此不可能。

Background playback should work just fine. 后台播放应该可以正常工作。 You do need the audio item in your UIBackgroundModes Info.plist entry, though. 但是,您确实需要UIBackgroundModes Info.plist条目中的audio项。 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. 另外,请确保您在设备上进行测试-我知道远程控制事件在iOS Simulator中不起作用,并且背景音频也可能如此。

The iOS SDK will automatically set up the AVAudioSession for you, so you don't need to do that. iOS SDK将自动为您设置AVAudioSession ,因此您无需这样做。

You might find this answer helpful, which is more in-depth: Background Audio with cocoalibspotify . 您可能会发现此答案很有用,它会更深入: 具有cocoalibspotify的背景音频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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