2016-12-26 1 views
2

백그라운드 재생으로 작동하는 Spotify iOS SDK를 가져올 수 없으므로 전화가 잠겨 있거나 응용 프로그램이 더 이상 작동하지 않을 때 트랙이 계속 재생됩니다.iOS SDK Spotify - 백그라운드 플레이 없음

Info.plistUIBackgroundModes는 이렇게 설정 한 :

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

는 내가 부족 뭔가 다른 또는 앱에 SDK를 설정할 때 내가 활성화해야 뭔가가 있나요? 어떤 도움

답변

9

로 작성 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) 
} 
0

에 미리

덕분에 나는 내 이전의 애플 리케이션 중 하나에 그것을 전에했던 생각합니다. 앱을 실행하자마자 오디오 세션을 구성해야한다고 생각하십시오.

다음은이를 수행하는 방법을 보여주는 코드입니다. 그러나이 AVAudioSession

1 단계

func audioStreaming(_ audioStreaming: SPTAudioStreamingController!, didChangePlaybackStatus isPlaying: Bool) { 
    if isPlaying { 
     self.activateAudioSession() 
    } else { 
     self.deactivateAudioSession() 
    } 
} 

에게 SPTAudioStreamingPlaybackDelegate를 구현하고 활성화하는 기능을 작성하고 비활성화 내 클래스를 확장하기 위해 내가 가진이 문제를 해결하려면 목적 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]; 
    } 
} 
+0

답변 주셔서 감사합니다. 배경 오디오로 작동하는 Spotify의 예제 프로젝트에서 아무 것도 없을 때 왜 이것이 필요한지 궁금합니다. –

+0

@ja 아무것도하지 않을 것이 확실합니까? 어떤 예를 들겠습니까? – christian

+0

SDK 다운로드에 포함 된 예제 프로젝트를 언급하고 있습니다. 비록 그들이 기본 동작의 일부가 아니기 때문에 AVAudioSession을 활성화시키는 무언가를 가지고 있다고 밝혀졌습니다. –

관련 문제