iphone detection headset Insert/unplug

Source: Internet
Author: User

During the development process, recording and playback this piece encountered some problems, the main trouble is three: detect if there is a sound input device when there are multiple sound output devices, the specified sound output device detects the insertion and removal of headphones

The first problem is that for devices such as itouch and ipads that do not have microphones themselves, they need to be checked for headphones with a recording function, which is relatively easy for the iphone because it has its own microphone. The second problem is that when an output device such as an ear machine is plugged into a device with its own speaker, multiple output devices are present, and it is necessary to specify where the sound is to be output in the program. The third problem is that inserting/unplugging headphones will inevitably cause changes in the sound output device, and if a headset with a microphone is inserted/unplugged on the itouch and ipad, it is bound to cause a change in the sound input device.

1. Detecting Sound Input devices

[plain] view plain copy print?   -(BOOL) Hasmicphone {return [[avaudiosession sharedinstance] inputisavailable]; } 2. Detecting Sound output devices
For the detection of output devices, we only consider 2 cases, one is the device itself (Itouch/ipad/iphone), one is currently inserted into the headset with the speaker. iOS has provided methods to get all of the current sound devices, and we just need to check if there are any of the ones we care about in these devices.
Get all current sound devices:
[plain] view plain copy print?   Cfstringref Route;   UInt32 propertysize = sizeof (CFSTRINGREF);   Audiosessiongetproperty (Kaudiosessionproperty_audioroute, &propertysize, &route); All possible sound devices on iOS include:

[CPP] view plain copy print?  /* Known values of route: * "Headset" * "Headphone" * "Speaker" * "speakerandmicrophone" * "Headphonesandmicrophone" * "Headsetinout" * "receiverandmicrophone" * "Lineout" * * For each item's specific device please refer to the iOS documentation, here we are concerned about whether there is a headset, so only need to check if there is a headpho in the route NE or headset exist, the method is as follows:

[Plain]  View plain copy print? -  (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"             */   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.