cocoa touch - How do you force audio to come out of the iPhone speaker when a headset is attached? -
i'm trying add loudspeaker feature 1 of iphone apps. created recording functionality, when play recorded audio plays phone headset.
what need recorded file played on loudspeaker, if there headset attached. how reroute audio this?
you need override default audio properties using audiosessionsetproperty
. @ force audio go speaker (note happen if headphones plugged in).
osstatus err = 0; uint32 audiorouteoverride = kaudiosessionoverrideaudioroute_speaker; err = audiosessionsetproperty(kaudiosessionproperty_overrideaudioroute,sizeof(audiorouteoverride),&audiorouteoverride);
to detect headphones, try (this literally copy/paste code off of post, caveat emptor, works me):
/** * tells if headset plugged in */ - (bool) headsetispluggedin { bool returnval = no; uint32 routesize = sizeof(cfstringref); cfstringref route = null; osstatus error = audiosessiongetproperty(kaudiosessionproperty_audioroute, &routesize, &route); if (!error && (route != null) && ([(nsstring*)route rangeofstring:@"head"].location != nsnotfound)) { cfrelease(route); returnval = yes; } return returnval; }
edit: there bit of discussion in comments whether cfrelease appropriate or not. hardcore core foundation experts care weigh in?
Comments
Post a Comment