如何在扬声器上播放audio,而不是在较弱的耳机上播放audio?

我正在学习核心audio。 出于某种原因,处理graphics的声音只能通过弱耳机扬声器(当您将设备固定在耳朵上)播放,而不能通过iPhone的常规扬声器。

这是设置audio会话的代码,但是我看不到configurationaudio路由的位置:

- (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 ); } 

在播放audio单元的声音时,您在核心audio的哪个点上说“play over speakers”?

您可以使用setCategory withOption:

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

我有同样的问题。 事实certificate,这与“播放和录制”类别有关。 只需要redirectaudio输出。

 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty ( kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride ); 

资源: