Spotify iOS SDK - 无后台播放
Spotify iOS SDK - No background play
我无法让 Spotify iOS SDK 与后台播放一起工作,以便在 phone 被锁定或应用程序不再处于活动状态时曲目继续播放。
我的 Info.plist
中有 UIBackgroundModes
设置如下:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
</array>
在应用程序中设置 SDK 时,是否还有其他遗漏或需要启用的内容?
在此先感谢您的帮助
我想我以前在我以前的一个应用程序中做过。认为您需要在应用启动后立即配置音频会话。
这里有一段代码展示了如何做到这一点。但是写在Objective C.
- (void) initializeAudioSession
{
// Registers this class as the delegate of the audio session to listen for audio interruptions
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(audioRouteChanged:)
name: AVAudioSessionRouteChangeNotification
object: [AVAudioSession sharedInstance]];
//Set the audio category of this app to playback (allows music to play in background)
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error: &setCategoryError];
if (setCategoryError) {
//RESPOND APPROPRIATELY
NSLog(@"AVAudioSession error: %@", setCategoryError);
}
// An instance of the audio player/manager is passed to the listener
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
//Activate the audio session
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if (activationError) {
//RESPOND APPROPRIATELY
NSLog(@"AVAudioSession error: %@", activationError);
}
}
#pragma mark -
#pragma mark Audio session callbacks
-(void)audioRouteChanged:(NSNotification*)audioChanged;
{
NSDictionary *userInfo = [audioChanged userInfo];
int routeChangeReason = (int)[userInfo objectForKey:AVAudioSessionRouteChangeReasonKey];
if ([SpotifyPlayer sharedPlayer].isPlaying) {
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
[[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil];
}
}
}
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue)
{
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
// "Old device unavailable" indicates that a headset was unplugged, or that the
// device was removed from a dock connector that supports audio output.
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
[[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil];
}
}
为了解决这个问题,我必须扩展我的 class 来实现 SPTAudioStreamingPlaybackDelegate
并写入函数来激活和停用 AVAudioSession
第 1 步
func audioStreaming(_ audioStreaming: SPTAudioStreamingController!, didChangePlaybackStatus isPlaying: Bool) {
if isPlaying {
self.activateAudioSession()
} else {
self.deactivateAudioSession()
}
}
第 2 步
// MARK: Activate audio session
func activateAudioSession() {
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try? AVAudioSession.sharedInstance().setActive(true)
}
// MARK: Deactivate audio session
func deactivateAudioSession() {
try? AVAudioSession.sharedInstance().setActive(false)
}
我无法让 Spotify iOS SDK 与后台播放一起工作,以便在 phone 被锁定或应用程序不再处于活动状态时曲目继续播放。
我的 Info.plist
中有 UIBackgroundModes
设置如下:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
</array>
在应用程序中设置 SDK 时,是否还有其他遗漏或需要启用的内容?
在此先感谢您的帮助
我想我以前在我以前的一个应用程序中做过。认为您需要在应用启动后立即配置音频会话。
这里有一段代码展示了如何做到这一点。但是写在Objective C.
- (void) initializeAudioSession
{
// Registers this class as the delegate of the audio session to listen for audio interruptions
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(audioRouteChanged:)
name: AVAudioSessionRouteChangeNotification
object: [AVAudioSession sharedInstance]];
//Set the audio category of this app to playback (allows music to play in background)
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error: &setCategoryError];
if (setCategoryError) {
//RESPOND APPROPRIATELY
NSLog(@"AVAudioSession error: %@", setCategoryError);
}
// An instance of the audio player/manager is passed to the listener
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
//Activate the audio session
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if (activationError) {
//RESPOND APPROPRIATELY
NSLog(@"AVAudioSession error: %@", activationError);
}
}
#pragma mark -
#pragma mark Audio session callbacks
-(void)audioRouteChanged:(NSNotification*)audioChanged;
{
NSDictionary *userInfo = [audioChanged userInfo];
int routeChangeReason = (int)[userInfo objectForKey:AVAudioSessionRouteChangeReasonKey];
if ([SpotifyPlayer sharedPlayer].isPlaying) {
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
[[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil];
}
}
}
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue)
{
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
// "Old device unavailable" indicates that a headset was unplugged, or that the
// device was removed from a dock connector that supports audio output.
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
[[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil];
}
}
为了解决这个问题,我必须扩展我的 class 来实现 SPTAudioStreamingPlaybackDelegate
并写入函数来激活和停用 AVAudioSession
第 1 步
func audioStreaming(_ audioStreaming: SPTAudioStreamingController!, didChangePlaybackStatus isPlaying: Bool) {
if isPlaying {
self.activateAudioSession()
} else {
self.deactivateAudioSession()
}
}
第 2 步
// MARK: Activate audio session
func activateAudioSession() {
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try? AVAudioSession.sharedInstance().setActive(true)
}
// MARK: Deactivate audio session
func deactivateAudioSession() {
try? AVAudioSession.sharedInstance().setActive(false)
}