【问题标题】:Spotify iOS SDK - No background playSpotify iOS SDK - 无后台播放
【发布时间】:2017-05-11 01:48:33
【问题描述】:

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

我的Info.plist 中有UIBackgroundModes 设置如下:

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

在应用中设置 SDK 时,我还缺少什么或需要启用什么?

提前感谢您的帮助

【问题讨论】:

    标签: ios spotify


    【解决方案1】:

    为了解决这个问题,我必须扩展我的类来实现 SPTAudioStreamingPlaybackDelegate 并写入函数来激活和停用 AVAudioSession

    第一步

    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)
    }
    

    【讨论】:

      【解决方案2】:

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

      这里有一段代码展示了如何做到这一点。但它是用 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];
          }
      }
      

      【讨论】:

      • 感谢克里斯蒂安的回答。我只是想知道为什么在 Spotify 的使用背景音频的示例项目中没有所有这些都是必要的?
      • @ja 你确定它什么都没做吗?你指的是哪个例子?
      • 我指的是SDK下载中包含的示例项目。事实证明他们确实有一些东西可以激活 AVAudioSession,尽管为什么它不是默认行为的一部分让我感到困惑
      • 我猜有些应用在发送到后台时需要停止播放。也许是让 SDK 适合所有场景的决定。
      • 我想,但是您只需在 .plist 文件中禁用背景模式。无论如何,感谢您的帮助!
      猜你喜欢
      • 2014-08-16
      • 2017-02-09
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2019-02-08
      • 2019-04-09
      • 2018-06-05
      • 2019-07-01
      相关资源
      最近更新 更多