2013-01-19 147 views
10

我正在學習核心音頻。出於某種原因,處理圖表的聲音只能通過弱耳機揚聲器播放(當您將設備放在耳邊時),而不是通過iPhone的常規揚聲器播放。如何通過揚聲器播放音頻,而不是較弱的耳機?

這是設置了音頻部分的代碼,但我無法看到它配置了音頻路線:

- (void) setupAudioSession { 

    AVAudioSession *mySession = [AVAudioSession sharedInstance]; 

    // Specify that this object is the delegate of the audio session, so that 
    // this object's endInterruption method will be invoked when needed. 
    [mySession setDelegate: self]; 

    // Assign the Playback category to the audio session. 
    NSError *audioSessionError = nil; 
    [mySession setCategory: AVAudioSessionCategoryPlayAndRecord//AVAudioSessionCategoryPlayback 
        error: &audioSessionError]; 

    if (audioSessionError != nil) { 

     NSLog (@"Error setting audio session category."); 
     return; 
    } 

    // Request the desired hardware sample rate. 
    self.graphSampleRate = 44100.0; // Hertz 

    [mySession setPreferredHardwareSampleRate: graphSampleRate 
             error: &audioSessionError]; 

    if (audioSessionError != nil) { 

     NSLog (@"Error setting preferred hardware sample rate."); 
     return; 
    } 

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

    if (audioSessionError != nil) { 

     NSLog (@"Error activating audio session during initial setup."); 
     return; 
    } 

    // Obtain the actual hardware sample rate and store it for later use in the audio processing graph. 
    self.graphSampleRate = [mySession currentHardwareSampleRate]; 

    // Register the audio route change listener callback function with the audio session. 
    AudioSessionAddPropertyListener (
     kAudioSessionProperty_AudioRouteChange, 
     audioRouteChangeListenerCallback, 
     self 
    ); 
} 

在這一點核心音頻,你說「過揚聲器播放」播放時聲音與音頻單元?

回答

4

我有同樣的問題。事實證明,這與「播放和錄製」類別有關。只需要重定向音頻輸出。

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 

AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute, 
    sizeof (audioRouteOverride), 
    &audioRouteOverride 
); 

來源:

+0

試過,但它沒有工作,user523234的解決方案是爲我工作。 – blackmoon

6

可以使用setCategorywithOption:

[mySession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&audioSessionError]; 
+0

謝謝你,它完美的作品。 – blackmoon

相關問題