에 미리
덕분에 나는 내 이전의 애플 리케이션 중 하나에 그것을 전에했던 생각합니다. 앱을 실행하자마자 오디오 세션을 구성해야한다고 생각하십시오.
다음은이를 수행하는 방법을 보여주는 코드입니다. 그러나이 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];
}
}
답변 주셔서 감사합니다. 배경 오디오로 작동하는 Spotify의 예제 프로젝트에서 아무 것도 없을 때 왜 이것이 필요한지 궁금합니다. –
@ja 아무것도하지 않을 것이 확실합니까? 어떤 예를 들겠습니까? – christian
SDK 다운로드에 포함 된 예제 프로젝트를 언급하고 있습니다. 비록 그들이 기본 동작의 일부가 아니기 때문에 AVAudioSession을 활성화시키는 무언가를 가지고 있다고 밝혀졌습니다. –