2011-02-22 27 views
2

我試圖解決AVAudioSession問題,因爲很多小時前,並沒有得到成功!AVAudioSession endInterruption()返回一個NSOSStatusErrorDomain

我發現很多人談論與endInterruption問題,但沒有人談論這個錯誤:

Unable to reactivate the audio session after the interruption ended: Error Domain=NSOSStatusErrorDomain Code=560161140 "The operation couldn’t be completed. (OSStatus error 560161140.)" 

我試着將這段代碼爲ASCII檢查音頻碼結果,但並沒有什麼關係這個號碼。

我的代碼初始化會話:

- (void) setupAudioSession { 

mySession = [AVAudioSession sharedInstance]; 
[mySession setDelegate: self]; 

NSError *audioSessionError = nil; 
[mySession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError]; 

if (audioSessionError != nil) { 
    NSLog (@"Error setting audio session category: %@", [audioSessionError description]); 
    return; 
} 

// Activate the audio session 
[mySession setActive: YES error: &audioSessionError]; 

if (audioSessionError != nil) { 
    NSLog (@"Error activating audio session during initial setup: %@", [audioSessionError description]); 
    return; 
} 

// Prepare audio file to play 
NSBundle *mainBundle = [NSBundle mainBundle]; 
NSURL *bgSoundURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"bg_sound" ofType:@"aif"]]; 

bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgSoundURL error:&audioSessionError]; 
if (!bgPlayer) { 
    NSLog(@"No bgPlayer: %@", [audioSessionError description]); 
} 

[bgPlayer setNumberOfLoops:-1]; 
[bgPlayer prepareToPlay]; 

}

而且endInterruption方法的代碼:

- (void) endInterruptionWithFlags: (NSUInteger) flags { 

if (flags & AVAudioSessionInterruptionFlags_ShouldResume) { 

    NSError *endInterruptionError = nil; 
    [[AVAudioSession sharedInstance] setActive: YES error: &endInterruptionError]; 
    if (endInterruptionError != nil) { 

     NSLog (@"Unable to reactivate the audio session after the interruption ended: %@", [endInterruptionError description]); 
     return; 

    } else { 

     NSLog (@"Audio session reactivated after interruption."); 

     if (interruptedWhilePlaying) { 

      self.interruptedWhilePlaying = NO; 

      // Resume playback by sending a notification to the controller object, which 
      // in turn invokes the playOrStop: toggle method. 
      NSString *MixerHostAudioObjectPlaybackStateDidChangeNotification = @"MixerHostAudioObjectPlaybackStateDidChangeNotification"; 
      [[NSNotificationCenter defaultCenter] postNotificationName: MixerHostAudioObjectPlaybackStateDidChangeNotification object: self]; 

     } 
    } 
} 

}

這段代碼的很大一部分提取來自Apple Mixer主機示例。 這很奇怪,樣本工作正常!

你們可以弄清楚什麼是錯的?

謝謝。

回答

3

我解決了改變設計音頻代碼方式的問題。 而不是使用AVAudioSession及其委託方法,我更改爲C風格以使用音頻。

我實現的功能:

void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) {} 

它初始化所用:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self); 

我裏面 - (ID)init方法。

希望它也可以幫助你。 最好。

2

"If this delegate method receives the AVAudioSessionInterruptionFlags_ShouldResume constant in its flags parameter, the audio session is immediately ready to be used."

您沒有正確處理回調。當您收到AVAudioSessionInterruptionFlags_ShouldResume時,您的音頻會話已準備好使用。當你得到一個不同的標誌時,你需要調用setActive。

希望它可以幫助...

1

使用@Trinca答案是什麼,我做了「ARC'ed」項目:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, (__bridge void *)(self.player)); 

是self.player是您的播放器實例,你傳遞給回調函數。

然後實現回調:

void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) { 
    NSLog(@"itnerp state %li",interruptionState); 
    NSLog(@"inUserData %@",inUserData); 
    AVAudioPlayer *pl = (__bridge AVAudioPlayer*)inUserData; 
    switch (interruptionState) { 
    case 0: 
     [pl play]; 
    break; 

    case 1: 
     [pl pause]; 
    break; 
    } 

} 

GOOD LUCK

沙尼

0

使用audiosession.setmode如果是VOIP