Recording and playback in the development process there are a number of problems, the main problem is three: detect whether there is a sound input device when there are multiple sound output devices, the specified sound output device detects the insertion and pull-out of headphones
First, for devices such as itouch and the ipad that do not have a microphone, you need to check to see if the headset is plugged in and it's relatively easy for the iphone because it's close to bringing a microphone. The second problem is that when an output device such as a headset is plugged into an outgoing device, a plurality of output devices appear, requiring the implementation to specify where the sound is to be exported in the program. Third, inserting /unplugging headphones necessarily causes a change in the sound output device, and if a headset with a microphone is inserted /unplugged on itouch and the ipad, it will inevitably cause changes in the sound input device.
1. Detect Sound input devices
[Plain] view plaincopyprint? -(BOOL) Hasmicphone {return [[avaudiosession sharedinstance] inputisavailable]; } 2. detecting Sound output devices
For the output device detection , we only consider 2 cases, one is the device itself out (Itouch/ipad/iphone), one is currently inserted with out headphones . iOS has already provided a way to get all of the current sound devices, and we just need to check to see if we have the ones we care about in these devices.
Get all current sound devices:
[Plain] view plaincopyprint? Cfstringref Route; UInt32 propertysize = sizeof (CFSTRINGREF); Audiosessiongetproperty (Kaudiosessionproperty_audioroute, &propertysize, &route); All possible sound devices on iOS include:
[CPP] view Plaincopyprint? /* Known values of route: * "Headset" * "Headphone" * "Speaker" * "speakerandmicrophone" * "Headphonesandmicrophone" * "Headsetinout" * "receiverandmicrophone" * "Lineout" * * for each specific representative of the equipment please refer to the iOS documentation, we are concerned about whether there are headphones , so only need to check in the route Whether there are headphone or headset, the specific methods are as follows:
[Plain] view plaincopyprint? - (BOOL) hasheadset { #if TARGET_IPHONE_SIMULATOR #warning *** Simulator mode: audio session code works only on a device return NO; #else CFStringRef route; uint32 propertysize = sizeof ( CFSTRINGREF); audiosessiongetproperty (Kaudiosessionproperty_audioroute, &propertysize, &route); if ((Route == NULL) | | (Cfstringgetlength (route) == 0)) { / / silent mode nslog (@ "audioroute: silent, do nothing!"); } else { NSString* routeStr = (nsstring*) route; nslog (@ "audioroute: %@", routestr); /* Known values of route: * "Headset" * " Headphone " * " Speaker " * "Speakerandmicrophone" * "Headphonesandmicrophone" * "Headsetinout" * "Receiverandmicrophone" * "Lineout" */ nsrange headphonerange = [routestr rangeofstring : @ "Headphone"]; nsrange headsetrange = [routestr rangeofstring : @ "Headset"]; if (Headphonerange.location != nsnotfound) { return YES; } else if (headsetrange.location != Nsnotfound) { return YES; &NBsp; } } return NO; #endif } Please note that Because the method of obtaining audioroute can no longer be run on simulator (directly crush), it must be handled in advance.
3. Set the sound output device
In our project, there is a user who is playingInsertor pull outHeadphonesof the situation. If the user is playingInsertHadHeadphones, Apple will automatically point the sound output toHeadphonesand automatically adjusts the volume to the appropriate size;HeadphonesIn the playback process, the user pulls outHeadphones, the sound automatically broadcasts from the device itself, but its volume does not automatically increase.
After our tests, we found out when playingHeadphonesThere will be two problems (maybe not a problem for you, but it will affect our app):
Music Playback Auto Stop
The sound volume does not automatically become larger, and the system still has a smaller sound (HeadphonesThe right sound) to carry out the
The first question, in fact, is the need to be able toDetectionToHeadphonesPull out of the event; and the second problem is the need to beHeadphonesForces the system output device to be set out on the system when unplugged.
To force a system sound output device to be modified:
[Plain]View Plaincopyprint? -(void) Resetoutputtarget {BOOL hasheadset = [self hasheadset]; NSLog (@ "'ll Set output target is_headset =%@.", Hasheadset @ "yes": @ "NO"); UInt32 audiorouteoverride = Hasheadset?