繁体   English   中英

Spotify iOS SDK - 没有后台播放

[英]Spotify iOS SDK - No background play

我无法让Spotify iOS SDK使用后台播放功能,以便在手机锁定或应用程序不再处于活动状态时继续播放曲目。

我在我的Info.plist设置了UIBackgroundModes ,如下所示:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
    <string>fetch</string>
</array>

在应用程序中设置SDK时,还有其他我缺少的东西或我需要启用的东西吗?

在此先感谢您的帮助

为了解决这个问题,我不得不扩展我的类来实现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)
}

我想我之前在之前的一款应用程序中做过这件事。 认为您需要在应用启动后立即配置音频会话。

这里有一些代码说明如何做到这一点。 但它是用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];
    }
}

暂无
暂无

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

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