Things to do with iOS recording and playback: a few wonderful needs

Source: Internet
Author: User

Recently in the project, you need to pick up the iphone with a headset (Apple called headset, without a Mai headset called headphone), and then achieve simultaneous recording and playback. After this feature is implemented, you need to change the recording and playback input sources.

For the realization of synchronous recording and playback function, it is necessary to use the interface to the bottom, with Avaudiorecorder/avaudioplayer is not possible.

After I studied the knowledge of iOS sound processing, I found that I was too troublesome, need to include at least Audioqueue/audiobuffer and so on, and then there are all kinds of complex C Struct, callback function, callback processing and so on, very troublesome, accidentally error, It's hard to find the wrong place. So I found a relatively well-known closed library on the internet, Novocaine. It is good for the input and output of sound, but also provides a very simple and definite callback function, I just need to modify the callback function, this is not a table.

Subsequent projects need to determine whether an input/output mode can be implemented: when the headset with the microphone is plugged in, the sound is entered from the headset microphone and then synced from the phone's built-in speakers.

After the phone is plugged into the headset, there are 2 input sources: a built-in microphone (build-in microphone), a headphone microphone (Wired microphone), and 3 output sources: On the phone, on the above output (built-in Receiver), Headphones (Wired headphones) and the built-in speakers (built-in Speaker) used to play the music.

After checking Apple's documentation, it was found that, according to Apple's product strategy, after inserting the headset, the user will automatically switch the input source to the headset microphone and switch the output source to the headset. Also, switching the output source to the headset is mandatory when the input source is the headset microphone. This is a bit of a detour, I give an example. If the input source is now the headset microphone, then the input source can only be headphones. If you switch the input source to the phone's built-in microphone (which is achievable), you can switch the output source to the phone.

Apple believes that inserting headphones on behalf of the user only wants to hear the sound from the headset. To protect the privacy of the user, Apple does not allow developers to change the output source randomly. That said, while Apple offers 3 ways to switch the output source (which is cumbersome and may be written later), the output source cannot be switched to the built-in speakers when the user plugs into the headset and we need the input source to be the headset microphone. Here is a more detailed description:http://stackoverflow.com/questions/5931799/ Redirecting-audio-output-to-phone-speaker-and-mic-input-to-headphones


The second problem is that you need to adjust the input source to the phone's built-in microphone while the headset is plugged in, while the output source remains the headset.


In the Avaudiosession class of thousand energy (not omnipotent), a few good methods are provided:

To print the input/input source that is currently working:

nsarray* input = [[Avaudiosession sharedinstance] currentroute].inputs; nsarray* output = [[Avaudiosession sharedinstance] currentroute].outputs; NSLog (@ "Current intput:%@", input); NSLog (@ "Current output:%@", output);

Print the currently available input sources:

nsarray* availableinputs = [[Avaudiosession sharedinstance] availableinputs]; NSLog (@ "Available inputs:%@", availableinputs);

Avaudiosession does not provide a list of available output sources.

After inserting the headset, print the available input sources and the results are as follows:

2015-01-26 17:45:40.747 PREGNOTICE[605:6D13] Available inputs: ("<avaudiosessionportdescription:0x16dc2de0, type = Microphonebuiltin; name = IPhone \u9ea6\u514b\u98ce; UID = built-in microphone; Selecteddatasource = \u4e0b> "," <avaudiosessionportdescription:0x16f40e80, type = microphonewired; name = \U8033\U673A\U9EA6\U514B\U98CE; UID = Wired microphone; Selecteddatasource = (null) > ")
As you can see, the input sources available now include a built-in microphone and a headset microphone (Wired microphone). We change the input source in the following ways:

nsarray* Inputarray = [[Avaudiosession sharedinstance] availableinputs];for (avaudiosessionportdescription* desc in Inputarray) {    if ([Desc.porttype isequaltostring:avaudiosessionportbuiltinmic]) {        nserror* error;        [[Avaudiosession sharedinstance] Setpreferredinput:desc error:&error];    }}
Again, when you need to switch back, check that Desc.porttype is not avaudiosessionportheadsetmic, and if so, adjust it back.

It is important to note that using this method triggers the avaudiosessionroutechangenotification notification, which is also invoked when the headset is plugged in. I was in the test, in order to check the headphone insertion action, listen to the notification, and then in the notification callback method through the above method to modify the input source, resulting in a notification triggered, so inserted 1 times the headset caused 2 calls.

There's a lot to know about sound and avaudiosession. Interested students, need to take care of the iOS voice students, must learn to learn.

Things to do with iOS recording and playback: a few wonderful needs

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.