How Do I Route Audio to Speaker without using AudioSessionSetProperty?
Asked Answered
M

4

37

As AudioSessionSetProperty may become deprecated, I'm trying to find an code example of how to route audio to the speaker using other means.

Previously I did the following:

-(void)setSpeakerEnabled
{
    debugLog(@"%s",__FUNCTION__);
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

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

Trying to get same result but without call to AudioSessionSetProperty.

Mcginty answered 14/9, 2013 at 22:45 Comment(6)
This question appears to be off-topic because it is about iOS 7 which is under NDA.Geriatrics
Wrong. You say AudioSessionGetProperty is deprecated, but it is available in iOS 6. It's deprecated in iOS 7. That's breaking the NDA.Geriatrics
@ColeJohnson, IMHO it is not the place of Stack Overflow to police NDAs.Sidestroke
footnote to previous commentSidestroke
@ColeJohnson, I was viewing the 2012 WWDC which states it is deprecated at 13:58 Session 505 Audio Services Deprecated. I will not discuss why I was watching it however.Mcginty
@Jim hmm. Well, my mistake then. I looked up the method on ADC (iOS 6 docs) and it doesn't say it's deprecated as far as I could tell.Geriatrics
S
61

On each release of iOS, more of the audioSession properties are migrated to AVFoundation, so you should use those in preference whenever available.

Since iOS 6 kAudioSessionProperty_OverrideAudioRoute is represented in AVAudioSession by the method

- (BOOL)overrideOutputAudioPort:error:

Available values are AVAudioSessionPortOverrideNone and AVAudioSessionPortOverrideSpeaker

Here is an example audio session configured entirely via AVFoundation:

 - (void)configureAVAudioSession
{
   // Get your app's 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

Since iOS 7.0, the Audio Session Services C API is now fully deprecated in favour of AVAudioSession.

UPDATE 2

- (BOOL)overrideOutputAudioPort:error:  

is a method, not a property, and it sets an underlying write-only UInt32 value. You can't get the current value, and you should treat the method as setting a temporary state. If the audio route changes or is interrupted, the property resets to its default (AVAudioSessionPortOverrideNone). You can get interruption notifications via AVAudioSessionDelegate.

Sidestroke answered 15/9, 2013 at 1:41 Comment(10)
How would this work for kAudioSessionProperty_OverrideCategoryEnableBluetoothInput in iOS 7 ?Tamra
@SjoerdPerfors - You would use the AVAudioSession method - (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError with category as AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord and options as AVAudioSessionCategoryOptionAllowBluetoothSidestroke
Thanks Foundry but that is not the same because that would allow A2DP bluetooth only. And this wouldn't force using it either. See also: #19135412Tamra
not work for me! my code: AVAudioSessionPortOverride port = (onOff ? AVAudioSessionPortOverrideSpeaker : AVAudioSessionPortOverrideNone); success = [session overrideOutputAudioPort:port error:&error];Lowenstern
@Lowenstern - perhaps you can make a new question showing more of your code... if it's returning an error, show the errorSidestroke
How to get current value of overrideOutputAudioPort?Bellinger
If anyone else is wondering why this doesn't work for them. You need to call it immediately before playing something. So [[AVAudioSession] sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&overrideError]; [_player play];Secrest
@foundry, even if my app is only playing back a buffer for live audio synthesis will audioSession category still have to be set to AVAudioSessionCategoryPlayAndRecord to use audioRouteOverride ? why not AVAudioSessionCategoryPlayback ?Corset
@Greg: Yes - because Apple says so. The AVAudioSessionPortOverrideSpeaker option only works when the audioSession is set to AVAudioSessionCategoryPlayAndRecord. Documented hereSidestroke
@foundry, I eventually got audio synthesis working again once I added AVFoundation.framework.Corset
W
7
NSError *error;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
                                                   error:&error];    
if(error)
{ 
    NSLog(@"Error: AudioSession cannot use speakers");
}
Weird answered 25/11, 2015 at 15:40 Comment(0)
V
2
let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
    } catch {
        print("error.")
    }  

// paste this code in your viewLoad region

Vicious answered 7/11, 2018 at 13:30 Comment(0)
C
1

Foundry’s solution together with this blog by Mario Diana has also allowed me to upgrade audio session set up code deprecated in iOS 7. My old code was based on AudioBufferPlayer by Matthijs Hollemans. Remember to add the AVFoundation.framework.

Corset answered 3/6, 2016 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.