IOS Audio headset with speaker switch

Source: Internet
Author: User

On the iphone there are two modes of playing the external voice: handset mode and microphone mode, the handset is of course the use of the phone, the voice of course is very small,

However, in the open, iOS default is this mode, so when the play outside should be added code to reset, as follows: Avaudioplayer * audioPlayer1 = [[Avaudioplayer alloc] Initwithcontentsofurl:soundurl Error:nil];

Audioplayer1.volume = 1.0;

[AudioPlayer1 Preparetoplay];

UInt32 Audioroute = Kaudiosessionoverrideaudioroute_speaker;

Audiosessionsetproperty (Kaudiosessionproperty_overrideaudioroute, sizeof (Audioroute), &audioroute);// These two lines of code are set to the microphone mode

[AudioPlayer1 play];

In turn a detailed: http://blog.csdn.net/xy5811/article/details/8563137

Select a category

AVAudioSessionCategoryAmbientor kAudioSessionCategory_AmbientSound --for non-voice-based applications, apps that use this category will be muted with the mute key and the screen off. It does not stop other apps from playing sound, and can play sounds with other apps such as Ipod,safari.

AVAudioSessionCategorySoloAmbientor kAudioSessionCategory_SoloAmbientSound --similar to avaudiosessioncategoryambient, the difference is that it stops other apps from playing sounds. This category is the default category.

AVAudioSessionCategoryPlaybackor kAudioSessionCategory_MediaPlayback --for voice-based applications, apps that use this category do not mute with the mute key and the screen off.

AVAudioSessionCategoryRecordOr kAudioSessionCategory_RecordAudio———For apps that need to be recorded, after setting the category, other system sounds except call tones, alarms or calendar reminders will not be played.

AVAudioSessionCategoryPlayAndRecordor kAudioSessionCategory_PlayAndRecord -for applications that need to play sound and require recording, voice chat applications (such as) should use this category.

AVAudioSessionCategoryAudioProcessingor kAudioSessionCategory_AudioProcessing———— when you need to use this category for offline speech processing, I don't quite understand what the offline speech processing concept is, and I hope to have the knowledge to explain it.

Note : Not an app can only use a category, the program should be based on the actual need to switch the different category, for example, when recording, it needs to be set to Avaudiosessioncategoryrecord, When the recording is finished, you should change the category to Avaudiosessioncategoryambient according to the program One of the avaudiosessioncategorysoloambient or Avaudiosessioncategoryplayback.

Set category

    1. <span style= "FONT-SIZE:14PX;" >nserror *setcategoryerror = nil;  
    2.     BOOL&NBSP;SUCCESS&NBSP;=&NBSP;[[AVAUDIOSESSION&NBSP;SHAREDINSTANCE]&NBSP;&NBSP;
    3. Li class= "alt" >                     setcategory: avaudiosessioncategoryambient  
    4.                      error:  &setcategoryerror];  
    5. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
    6.     if  (!success)  { /*  handle the error in setcategoryerror */ }</span>  

Switch to Speaker

As Apple's official document says, it's only when the category is set to Avaudiosessioncategoryplayandrecord that the sound is played from the speakers, which I haven't confirmed yet.

In summary, according to the official documentation, first set the category to Avaudiosessioncategoryplayandrecord, and then redirect the audio by rewriting the audio route property.

The Audio Route property has the following two, one is the default handset and the other is the speaker.

    1. enum {
    2. Kaudiosessionoverrideaudioroute_none = 0,
    3. Kaudiosessionoverrideaudioroute_speaker = ' SPKR '
    4. };
There are two ways to Override audio route:

  1. UInt32 audiorouteoverride = Kaudiosessionoverrideaudioroute_speaker;
  2. Audiosessionsetproperty (
  3. Kaudiosessionproperty_overrideaudioroute
  4. sizeof (audiorouteoverride),
  5. &audiorouteoverride
  6. );

  1. UInt32 Dochangedefaultroute = 1;
  2. Audiosessionsetproperty (
  3. Kaudiosessionproperty_overridecategorydefaulttospeaker,
  4. sizeof (Dochangedefaultroute),
  5. &dochangedefaultroute
  6. );

The difference between the two methods is:

When using Kaudiosessionproperty_overrideaudioroute, the audio route will reset back to the handset when any interrupts such as plug-in headphones have occurred and you must set it again.

Use Kaudiosessionproperty_overridecategorydefaulttospeaker unless you change the category, it will remain in effect.

IOS Audio headset with speaker switch

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.