IOS avaudiosession configuration (sound becomes smaller after recording)

Source: Internet
Author: User



There is such a scene, first we recorded, recording and then playback found that the volume has become smaller;



Best of all, view API Discovery Avaudiosession There is an option



If your app involves audio and video calls and plays other sounds, you can look at the following configuration when the sound gets smaller.



Avaudiosessioncategoryoptionduckothers



The Apple document says that if Avaduiosession is configured like this, the other sounds will be smaller in the current scenario.



For example, when using the mobile phone to play music, then when the navigation of the sound play, the sound of music needs to be small, to achieve the navigation of the voice is not affected;



After the navigation sound plays, we need to make the sound of the music back to normal, then it can be reconfigured to activate;



The current scene can also use two players, directly control the volume to achieve;



The following code


in our audio and video scene configuration, specifies that other sounds are forced to become smaller    avaudiosession *ses = [avaudiosession sharedinstance];    [SES setcategory:avaudiosessioncategoryplayandrecord  withoptions:avaudiosessioncategoryoptionduckothers Error:nil]; // When our scene ends, the sound becomes smaller in order not to affect other scenes;    avaudiosession *ses = [Avaudiosession sharedinstance]; [ses setactive:no error:nil]; [SES setcategory:avaudiosessioncategoryplayandrecord withoptions:avaudiosessioncategoryoptiondefaulttospeaker Error:nil]; [ses setactive:yes error:nil];    





I. Configuring the Avaudiosession interface


/**/-(BOOL) setcategory: (NSString *) Category error: (Nserror *) Outerror; / **/-(BOOL) setcategory: (NSString *) category withoptions: (avaudiosessioncategoryoptions) Options Error: ( Nserror * *) Outerror Ns_available_ios (6_0); / **/-(BOOL) setcategory: (NSString *) Category mode: (NSString *) mode options: (avaudiosessioncategoryoptions) Options Error: (Nserror *) Outerror Ns_available_ios (10_0);      


Two. Turn off and activate the Avaudiosession configuration interface


/* Set the session active or inactive. Note that activating an audio session is a synchronous (blocking) operation.  Therefore, we recommend that applications not activate their session from a thread where a long blocking operation would be Problematic.  Note that this method would throw a exception in apps linked on or after IOS 8 if the session was set inactive while it had Running or  paused I/O (e.g. audio queues, players, recorders, converters, remote I/Os, etc). */-(BOOL) SetActive: (BOOL) Active error: (nserror * *) outerror;-(BOOL) SetActive: (bool) Active Withoptions: ( avaudiosessionsetactiveoptions) Options Error: (Nserror *) Outerror Ns_available_ios (6_0);  


Three. Some configuration options for audio development


Avaudiosessioncategory
    1. AVAudioSessionCategoryAmbient

      The current app's play sounds can coexist with the sounds played by other apps, stopping when the lock screen or press mute.

    2. AVAudioSessionCategorySoloAmbient

      Only the sound of the current app can be played, and the rest of the app's sounds will stop when the lock screen or press mute is stopped.

    3. AVAudioSessionCategoryPlayback

      Only the sound of the current app can be played, and the rest of the app's sounds will stop and will not stop when the lock screen or press mute.

    4. AVAudioSessionCategoryRecord

      Can only be used for recording, other app sounds will stop and will not stop when the lock screen or press Mute

    5. AVAudioSessionCategoryPlayAndRecord

      Play other sounds while recording, and do not stop when the lock screen or press Mute

    6. AVAudioSessionCategoryAudioProcessing

      Use a hardware decoder to process audio that cannot be played or recorded during the use of the audio session

    7. AVAudioSessionCategoryMultiRoute

      A variety of audio input and output, such as headphones, USB devices can play simultaneously

Avaudionsessionmode
    1. AVAudioSessionModeDefault

      default mode, suitable for all scenarios

    2. AVAudioSessionModeVoiceChat

      Applicable category Avaudiosessioncategoryplayandrecord, application scenario VoIP

    3. AVAudioSessionModeGameChat

      Applicable category Avaudiosessioncategoryplayandrecord, application scenario game recording, automatically set by Gkvoicechat, no need to call manually

    4. AVAudioSessionModeVideoRecording

      Applicable category Avaudiosessioncategoryplayandrecord,avaudiosessioncategoryrecord video recording of application scenarios

    5. AVAudioSessionModeMoviePlayback

      Applicable category Avaudiosessioncategoryplayback application scenario video playback

    6. AVAudioSessionModeVideoChat

      Applicable category Avaudiosessioncategoryplayandrecord, Video call for application scenario

    7. AVAudioSessionModeMeasurement

      Applicable category Avaudiosessioncategoryplayandrecord,avaudiosessioncategoryrecord,avaudiosessioncategoryplayback

    8. AVAudioSessionModeSpokenAudio

      IOS9 new additions to the

Avaudiosessioncategoryoptions
  1. AVAudioSessionCategoryOptionMixWithOthers

    Suitable for Avaudiosessioncategoryplayandrecord, Avaudiosessioncategoryplayback, and Avaudiosessioncategorymultiroute, Used to mix with other apps

  2. AVAudioSessionCategoryOptionDuckOthers

    Suitable for avaudiosessioncategoryambient, Avaudiosessioncategoryplayandrecord, Avaudiosessioncategoryplayback, and Avaudiosessioncategorymultiroute, used to lower the volume of other sound playback, making the period volume smaller

  3. AVAudioSessionCategoryOptionAllowBluetooth

    For Avaudiosessioncategoryrecord and Avaudiosessioncategoryplayandrecord, for Bluetooth device headset support, etc.

  4. AVAudioSessionCategoryOptionDefaultToSpeaker

    For Avaudiosessioncategoryplayandrecord, used to play sound from speaker, speaker, i.e. hands-free

  5. AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers

    For Avaudiosessioncategoryplayandrecord, Avaudiosessioncategoryplayback, and Avaudiosessioncategorymultiroute, iOS9 The newly added

  6. AVAudioSessionCategoryOptionAllowBluetoothA2DP

    Suitable for Avaudiosessioncategoryplayandrecord, Bluetooth and A2DP

  7. AVAudioSessionCategoryOptionAllowAirPlay

    Suitable for Avaudiosessioncategoryplayandrecord,airplay

Avaudiosessioncategoryoptionduckothers


When setting Categoryplayandrecord, set option to duckothers at the same time, which will depress the other volume playback



Workaround, reset.
This allows a application to set whether or is not the other active audio apps would be a ducked when your app ' s audio
Session goes active. A example of the app, which provides periodic updates to its user (it reduces the
Volume of any music currently being played while it provides its status). This is defaults to OFF. Note that the other
Audio would be ducked-as long as the current session is active. You'll need to deactivate your audio
Session when you want full volume playback of the other audio.  






Reference: HTTP://WWW.JIANSHU.COM/P/3E0A399380DF



IOS avaudiosession configuration (sound becomes smaller after recording)


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.