Audiosession Detailed category Select handset Speaker switch

Source: Internet
Author: User

Select a category

AVAudioSessionCategoryAmbientOrkAudioSessionCategory_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. Note: The category cannot play sound in the background

AVAudioSessionCategorySoloAmbientOrkAudioSessionCategory_SoloAmbientSound

-Similar to Avaudiosessioncategoryambient, the difference is that it stops other applications from playing sounds. This category is the default category. The category cannot play sound in the background

AVAudioSessionCategoryPlaybackOrkAudioSessionCategory_MediaPlayback

--for voice-based applications, apps that use this category do not mute with the mute key and the screen off. Sound can be played in the background

AVAudioSessionCategoryRecordOrkAudioSessionCategory_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. This category only provides a simple recording function.

AVAudioSessionCategoryPlayAndRecordOrkAudioSessionCategory_PlayAndRecord

--for applications that need to play both sound and recording, voice chat applications (such as) should use this category. This category provides recording and playback functions. If your app needs an iphone handset, the category is your only option, and the default exit for sound in that category is the handset (in the absence of an external device).

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

[OBJC]View Plaincopy
    1. Nserror *setcategoryerror = nil;
    2. BOOL success = [[Avaudiosession sharedinstance]
    3. Setcategory:avaudiosessioncategoryambient
    4. Error: &setCategoryError];
    5. if (!success) {/* Handle the error in Setcategoryerror */}

Activate & Deactivate Audiosession

[OBJC]View Plaincopy
    1. Nserror *error = nil;
    2. Avaudiosession *audiosession = [Avaudiosession sharedinstance];
    3. BOOL ret = [audiosession setactive:yes error:&error];
    4. if (!ret)
    5. {
    6. NSLog (@ "%s-activate Audio session failed with error%@", __func__,[error description]);
    7. }

[OBJC]View Plaincopy
    1. Nserror *error = nil;
    2. Avaudiosession *audiosession = [Avaudiosession sharedinstance];
    3. Note:set Avaudiosessionsetactiveoptionnotifyothersondeactivation to resume other apps ' audio.
    4. BOOL ret = [audiosession setactive:no withoptions:avaudiosessionsetactiveoptionnotifyothersondeactivation Error: &error];
    5. if (!ret)
    6. {
    7. }

Selection of Audio Route

When your iphone is connected to multiple external audio devices (earbuds, Bluetooth headsets, etc.), audiosession will follow the principle of last-in wins to select the external device, i.e. the sound will be directed to the last connected device.

When no audio device is plugged in, normally the sound comes out of the speaker by default, but with one exception: in PlayAndRecord This category, the handset becomes the default output device. If you want to change this behavior, you can provide Mpvolumeview to allow the user to switch to the speaker, or the Overrideoutputaudioport method to programmingly switch to the speaker, or you can modify the category option is Avaudiosessioncategoryoptiondefaulttospeaker.


Playandrecord Switch to the speaker

In addition to allowing users to manually select, you can also use the following two ways to switch in the program

1. Modify the default behavior of category:

[OBJC]View Plaincopy
    1. [Audiosession Setcategory:avaudiosessioncategoryplayandrecord withoptions: Avaudiosessioncategoryoptiondefaulttospeaker error:&error];
2. Overrideoutputaudioport:

[OBJC]View Plaincopy
    1. [Audiosession Overrideoutputaudioport:avaudiosessionportoverridespeaker error:&error];

Audiosession Detailed category Select handset 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.