2011-01-24 38 views
1

我在使用AVAudioRecorder的AVAudioSession在我正在開發的cocos2d遊戲中遇到問題。AVAudioSession自動改變iPhone音頻?

我試圖捕捉話筒輸入使用一個簡單的AVAudioRecorder示例來檢測用戶何時在麥克風中發出聲音(聲音本身並不重要,因爲我錄製到/ dev/null)。

下面是我的麥克風設置代碼:

NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"]; 

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 

    NSDictionary *recordSettings = 
    [[NSDictionary alloc] initWithObjectsAndKeys: 
    [NSNumber numberWithFloat: 22050.0], AVSampleRateKey, 
    [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
    [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
    [NSNumber numberWithInt: AVAudioQualityLow], 
    AVEncoderAudioQualityKey, 
    nil]; 

    micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil]; 
    [newURL release]; 
    [recordSettings release]; 
    [micInput setMeteringEnabled:YES]; 

在與上面的代碼的iPhone,現場所有的音頻(聲音效果,背景音樂等)開始於一個真正的打低水平,因爲它只是通過手機揚聲器而不是外置揚聲器播放。當我在iPad或iPod Touch上測試時,背景音頻按照預期通過外部揚聲器播放。這是一個問題,因爲在遊戲的這個特定部分玩iPhone版本時,遊戲的音量會急劇下降。

當我註釋掉AVAudioSession安裝線時,聲音會通過外部揚聲器播放,但當然我無法再獲得麥克風輸入。有什麼解決方法或解決方案來解決這個問題嗎?我需要能夠使用AVAudioRecorder進行錄製,但仍然可以從iPhone的外部揚聲器輸出音頻。

謝謝!

回答

2

嘗試像下面這樣您設置您的音頻會議後:

  UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker; 
     AudioSessionSetProperty (
      kAudioSessionProperty_OverrideAudioRoute, 
      sizeof (ASRoute), 
      &ASRoute 
     ); 
+0

這個解決我的問題。非常感謝您的幫助! – chmod