Method 1:
UInt32 Audioroute = Kaudiosessionoverrideaudioroute_speaker; Audiosessionsetproperty (Kaudiosessionproperty_overrideaudioroute, sizeof (Audioroute), &audioroute);
Method 2:
[[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback Error:nil];
Solution for Audiosessioninitialize Fail (
dispatch_once_t
) Solution Audioqueuestart failed (-50)
The
audiosessioninitialize can be executed multiple times, but Audiosessioninterruptionlistener can only be set once, which means that the interrupt callback method is a static method, Once the initialization is successful, all interrupts will be called back to this method, even though the next time the audiosessioninitialize is invoked again and another static method is passed as a parameter, it will be recalled to the first set method when the interrupt arrives. This scenario is not uncommon, such as your app , and of course you can't know which feature the user will call first. So you have to call the Audiosessioninitialize registration interrupt method in the playback and recording module, but the final interrupt callback will only function in the module of the earlier registration, the egg hurts ... So the best way to use the audiosession is to generate a class that manages it individually, receives the interrupt callback and sends a custom interrupt notification, receives the notification in the module that needs to use the audiosession, and takes the appropriate action. Excerpt from: http://www.cocoachina.com/industry/20140717/9162.html
Static dispatch_once_t Oncetoken; __block Osstatus error; Dispatch_once (&oncetoken, ^{error = audiosessioninitialize (null, NULL, nil, (__bridge void*) self); if (error) printf ("Error INITIALIZING AUDIO session! %d\n ", (int) error); else {UInt32 category = Kaudiosessioncategory_playandrecord; Error = Audiosessionsetproperty (kaudiosessionproperty_audiocategory, sizeof (category), &category); Audiosessionsetactive (TRUE); if (error) printf ("couldn ' t set audio category!"); Error = Audiosessionaddpropertylistener (Kaudiosessionproperty_audioroutechange, Proplistener, (__bridge void*) self) ; if (error) printf ("Error ADDING AUDIO SESSION PROP listener! %d\n ", (int) error); UInt32 inputavailable = 0; UInt32 size = sizeof (inputavailable); We don't want to allow recording if input are not availabLe error = audiosessiongetproperty (kaudiosessionproperty_audioinputavailable, &size, &inputavailable); if (error) printf ("Error GETTING INPUT availability! %d\n ", (int) error); We also need to listen to see if input availability changes error = Audiosessionaddpropertylistener (kaudiose Ssionproperty_audioinputavailable, Proplistener, (__bridge void*) self); if (error) printf ("Error ADDING AUDIO SESSION PROP listener! %d\n ", (int) error); Error = Audiosessionsetactive (true); if (error) printf ("Audiosessionsetactive (true) failed"); UInt32 Audioroute = Kaudiosessionoverrideaudioroute_speaker; Error = Audiosessionsetproperty (kaudiosessionproperty_overrideaudioroute, sizeof (Audioroute), &audioroute); if (error) printf ("Audiosessionsetproperty (Audioroute_speaker) failed"); } });
Audio playback after iOS recording is small, audiosessioninitialize Failed,audioqueuestart failed (-50)