How do I direct audio to a speaker without using the AudioSessionSetProperty function?

Since AudioSessionSetProperty can become deprecated , I am trying to find a code example on how to route audio to speaker using other means.

I previously did the following:

 -(void)setSpeakerEnabled { debugLog(@"%s",__FUNCTION__); UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty ( kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride ); } 

Trying to get the same result, but without call before AudioSessionSetProperty .

+36
ios objective-c avaudiosession
Sep 14 '13 at 22:45
source share
3 answers

In each iOS release, more audioSession properties are ported to AVFoundation, so you should use them as you see fit whenever it is available.

Since iOS 6 kAudioSessionProperty_OverrideAudioRoute is represented in the AVAudioSession method

 - (BOOL)overrideOutputAudioPort:error: 

Available Values: AVAudioSessionPortOverrideNone and AVAudioSessionPortOverrideSpeaker

Here is an example audio session that is fully configured with AVFoundation:

  - (void)configureAVAudioSession { // Get your app audioSession singleton object AVAudioSession *session = [AVAudioSession sharedInstance]; // Error handling BOOL success; NSError *error; // set the audioSession category. // Needs to be Record or PlayAndRecord to use audioRouteOverride: success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; if (!success) { NSLog(@"AVAudioSession error setting category:%@",error); } // Set the audioSession override success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; if (!success) { NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); } // Activate the audio session success = [session setActive:YES error:&error]; if (!success) { NSLog(@"AVAudioSession error activating: %@",error); } else { NSLog(@"AudioSession active"); } } 

UPDATE

Starting with iOS 7.0, the audio session APIs are now completely deprecated in favor of AVAudioSession.

UPDATE 2

 - (BOOL)overrideOutputAudioPort:error: 

is a method, not a property, and it sets the base value of UInt32 for writing. You cannot get the current value, and you should consider the method as setting a temporary state. If the audio route changes or is interrupted, the property is reset to the default value ( AVAudioSessionPortOverrideNone ). You can receive interrupt notifications through AVAudioSessionDelegate .

+58
Sep 15 '13 at 1:41
source share
— -
 NSError *error; [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; if(error) { NSLog(@"Error: AudioSession cannot use speakers"); } 
+4
Nov 25 '15 at 15:40
source share

The Foundrys solution, along with this blog from Mario Diana, also allowed me to update the audio session setup code deprecated in iOS 7. My old code was based on AudioBufferPlayer by Matthijs Hollemans . Remember to add AVFoundation.framework.

0
Jun 03 '16 at 2:00
source share



All Articles