IOS recording and playing: A few amazing demands

Source: Internet
Author: User

IOS recording and playing: A few amazing demands

In a recent project, you need to connect the iPhone with a microphone (Apple is called Headset, and without a microphone is called Headphone), and then synchronize the recording and play. After this function is implemented, you need to change the Input Source for recording and playing.

For Synchronous Recording and playback, you must use the underlying interface. AVAudioRecorder/AVAudioPlayer cannot be used.

After studying the knowledge of iOS sound processing, I found that it was too difficult to implement it. I needed at least AudioQueue/AudioBuffer and so on, then there are a variety of complex C Struct, callback functions, callback processing, and so on. It is very troublesome. If you accidentally make an error, it is difficult to find the error. So I found a well-known Closed library on the internet, Novocaine. It implements Sound Input and Output better, and provides a very simple and clear callback function. I only need to modify the callback function, which is not shown here.

The following project needs to determine whether an input/output method can be implemented: When a headset with a microphone is inserted, the sound can be input from the headset microphone, and then synchronized to play from the phone's built-in speaker.

After a Microphone headset is inserted into a mobile phone, there are two input sources: Build-In Microphone and Wired Microphone. Three output sources: used during phone calls, the above output (Built-In Speaker), the headset (Wired Headphones), and the built-in Speaker (Built-In Speaker) used for playing music ).

After checking Apple's documents, we found that, according to Apple's product policy, after inserting a microphone, the user will automatically switch the input source to the headset microphone and switch the output source to the headset. In addition, when the input source is a headset microphone, it is mandatory to switch the output source to the headset. This is an example. If the input source is a headset microphone, the input source can only be a headset. If you switch the input source to the phone's built-in microphone (this can be done), you can switch the output source to the phone.

Apple believes that inserting a headset means that the user only wants to listen to the sound from the headset. To protect user privacy, Apple does not allow developers to change the output source at will. That is to say, although Apple provides three methods for switching the output source (both of which are cumbersome and may be written later, in addition, when the input source is a headset microphone, the output source cannot be switched to the built-in speaker. 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 when the headset is inserted, while the output source remains the headset.


In the AVAudioSession class of qieneng (not omnipotent), several good methods are provided:

Print input/Input Source currently working:

NSArray* input = [[AVAudioSession sharedInstance] currentRoute].inputs;NSArray* output = [[AVAudioSession sharedInstance] currentRoute].outputs;NSLog(@"current intput:%@",input);NSLog(@"current output:%@",output);

Print available input sources:

NSArray* availableInputs = [[AVAudioSession sharedInstance] availableInputs];NSLog(@"available inputs:%@",availableInputs);

AVAudioSession does not provide a list of available output sources.

After the headset is inserted, the available input source is printed. The result is as follows:

2015-01-26 17:45:40.747 PregNotice[605:6d13] available inputs:("","")
As you can see, the available input sources include the built-in Microphone and the headset Microphone (Wired Microphone ). Use the following method to change the Input Source:

NSArray* inputArray = [[AVAudioSession sharedInstance] availableInputs];for (AVAudioSessionPortDescription* desc in inputArray) {    if ([desc.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {        NSError* error;        [[AVAudioSession sharedInstance] setPreferredInput:desc error:&error];    }}
Similarly, when you need to switch back, check whether desc. portType is AVAudioSessionPortHeadsetMic. If yes, adjust it back.

Note that using this method triggers the AVAudioSessionRouteChangeNotification notification, which is also called after the headset is inserted. During the test, I listened to the notification to check the earphone insertion action, and then modified the Input Source in the notification callback method. As a result, the notification was triggered again, therefore, the headset is inserted twice, resulting in two calls.

There is still much to know about sound and AVAudioSession. If you are interested in iOS, you must take a good course.

Related Article

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.