Avaudiosession Application Guide

Source: Internet
Author: User
Tags vars

Turn COCO-LG

Audiosession is responsible for adjusting the audio behavior in your app and iOS system. Once the audiosession is loaded you can get a single audiosession. You can configure this audiosession to control the audio behavior of your app. For example:

    • When your app plays sound, do you want to mute other apps that are playing sound or mix the sounds of two apps?
    • How does your app respond to bursts of interruptions, such as when the alarm suddenly rings?
    • How does your app respond to the plug-in of the headphone port?

The auiosession configuration will affect all audio activity in your app while running, in addition to audio controlled by the system Sounds Services API.

You can also use Audiosession to detect the parameters of the hardware you are using, such as channel and sample rate.

You can also activate or deactivate your audiosession at any time, and when your app plays a sound or is recording, you must make sure the audiosession is active.

The system also has the right to interrupt your audiosession at any time, for example, when you call. Of course, Audiosession provides the API to get your app to recover from this interruption.

First, the instantiation of

[OBJC]View PlainCopy
    1. Avaudiosession *audiosession = [avaudiosession sharedinstance];

You can see that Avaudiosession is a singleton object.

Second, the category setting

Once you have an instance of the Avaudiosession class, you can choose from the different categories available to your iOS app by invoking the Setcategory:error: instance method of the audio session object. Categories are divided into several types:

[OBJC]View PlainCopy
  1. #pragma mark--the Values for the category property--
  2. /* Use the This category for background sounds such as rain, car engine noise, etc.
  3. Mixes with other music. */
  4. Avf_export NSString *const avaudiosessioncategoryambient;
  5. /* Use the This category for background sounds. Other music would stop playing. */
  6. Avf_export NSString *const avaudiosessioncategorysoloambient;
  7. /* Use this category for music tracks.*/
  8. Avf_export NSString *const avaudiosessioncategoryplayback;
  9. /* Use the This category when recording audio. */
  10. Avf_export NSString *const Avaudiosessioncategoryrecord;
  11. /* Use the This category when recording and playing back audio. */
  12. Avf_export NSString *const Avaudiosessioncategoryplayandrecord;
  13. /* Use the This category if using a hardware codec or signal processor while
  14. Not playing or recording audio. */
  15. Avf_export NSString *const avaudiosessioncategoryaudioprocessing Ns_deprecated_ios (3_0, 10_  0) __tvos_prohibited __watchos_prohibited;

1.AVAudioSessionCategoryAmbient

is used for non-speech-based apps that use this category to mute the mute key and 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   2.AVAudioSessionCategorySoloAmbient  Similar to the avaudiosessioncategoryambient, the difference is that it stops other apps from playing sounds. This category is the default category. The category does not play sound   3.AVAudioSessionCategoryPlayback  for voice-based apps in the background, and apps that use this category don't mute with the mute key and the screen off. You can play sound in the background   4.AVAudioSessionCategoryRecord  for applications that require recording, and after you set up the category, other system sounds except call tones, alarms or calendar reminders will not be played. This category only provides a simple recording function.  5. AVAudioSessionCategoryPlayAndRecord  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 application 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.

 

Third, restore the external music

As you can see from the categories above, when recording or broadcasting, the external audio is being played off. For example, when the voice is being played, the concert is stopped. But after recording, you need to keep the external sound playing. This needs to be set manually :

[OBJC]View PlainCopy
    1. [[Avaudiosession sharedinstance] setActive:NO
    2. withflags:avaudiosessionsetactiveoptionnotifyothersondeactivation
    3. error: nil];

It is important to note that if there is no external sound playing before recording or broadcasting, the direct execution of this sentence will cause some of the sound effects in the cocos2d to be abnormal. So, before you start recording or broadcasting, determine if there is an external sound playback, then use a marker record, after the recording or broadcast completed to determine the mark bit, if there is external sound, then execute the upper code to restore external sound. Here's the code for judging the external sound:

[OBJC]View PlainCopy
    1. -(BOOL) isotheraudioplaying {
    2. UInt32 isplaying = 0;
    3. UInt32 varsize = sizeof (isplaying);
    4. Audiosessiongetproperty (kaudiosessionproperty_otheraudioisplaying, &varsize, &isPlaying);
    5. return (isplaying! = 0);
    6. }

Iv. coordinating the current application background sound

If we come across a situation where our app has background music, it needs to be recorded, and the background music will be resumed. This only needs to be set again the background music category, the general background music is the system default category:

[OBJC]View PlainCopy
    1. [[Avaudiosession sharedinstance] setcategory:avaudiosessioncategoryambient error: nil];

Five, the attention point

1. If there is no external sound playback before recording or broadcasting, the direct execution of this sentence will cause some of the sound effects in the cocos2d to be abnormal. Therefore, before starting the recording or broadcasting to determine whether there is an external sound playback, and then with a marker record, after the recording or broadcast completion of the logo to determine the position, if there is external sound, then execute the upper code to restore external sound;

2. When you have background music, perform the "Recover external music" warning deactivating an audio session the has running I/O

Please refer to the specific explanation: http://blog.csdn.net/gang544043963/article/details/53323270

Avaudiosession Application Guide

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.