iOS audio AVAudioSession related settings

1. AVAudioSession

AVAudioSession is used to manage resource usage of audio hardware devices (microphones, speakers) between multiple APPs.

For example, AVAudioSession can do these things

  • Set whether your APP exists at the same time as other APP audio, or interrupt other APP sound
  • When the mobile phone is set to silent mode, can the audio of your own APP play the sound?
  • Event handling when a phone or other APP interrupts the audio of its own APP
  • Specify the device for audio input and output (for example, whether the earpiece outputs sound or the speaker outputs sound)
  • Whether to support recording, whether to support audio playback while recording

2. AVAudioSession Category

Category defines seven main scenarios, and each category corresponds to whether it supports the following four capabilities.

  • Interrupts non-mixable apps audio: whether to interrupt apps that do not support audio mixing
  • Silenced by the Silent switch: Will it respond to the phone's mute key switch
  • Supports audio input: whether to support audio recording
  • Supports audio output: whether to support audio playback
Category Whether to allow audio playback/recording Whether to interrupt other apps that do not support mixing Whether it will be muted by the mute key or the lock screen key
AVAudioSessionCategorySoloAmbient Only supports playback yes yes
AVAudioSessionCategoryAmbient Only supports playback no yes
AVAudioSessionCategoryPlayback Only supports playback Default YES, can be rewritten to NO no
AVAudioSessionCategoryRecord Only supports recording yes No (recording is still possible from the lock screen)
AVAudioSessionCategoryPlayAndRecord Support playback, support recording Default YES, can be rewritten to NO no
AVAudioSessionCategoryAudioProcessing Does not support playback, does not support recording yes no
AVAudioSessionCategoryMultiRoute Support playback, support recording yes no

 

When the APP starts, it will automatically activate AVAudioSession (the default is AVAudioSessionCategorySoloAmbient)

#import <AVFoundation/AVFoundation.h>


/*
 * APP启动后激活声道音频模式
 **/
+ (void)configAvSessionCategoryWithError:(NSError **)error {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    
    //AVAudioSessionCategorySoloAmbient是系统默认的category
    [session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
    
    //激活AVAudioSession
    [session setActive:YES error:nil];
}

 

3. Example of pausing and resuming background audio playback

If you need to pause the music playback of other apps in the background while playing the audio in your own app;

When the playback is complete, resume the automatic playback of other audio in the background;

code show as below:

#import <AVFoundation/AVFoundation.h>

 
/*
 * 比如临时视频播放等APP,需要播放自己的音乐,暂停后台背景音乐的播放(激活当前应用的audio)
 **/
+ (void)pauseOtherAppSoundWithError:(NSError **)error {
        
    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayback error:error];//仅播放时
    //[session setCategory:AVAudioSessionCategoryRecord error:nil];//需要录音时

    //Activate audio session in current app
    //Deactivate audio session in others' app
    [session setActive:YES error:error];
}


/*
 * 自己APP停止播放后,继续恢复后台其他APP背景音乐的播放(取消激活当前应用的audio session)
 **/
+ (void)resumeOtherAppSoundWithError:(NSError **)error {    
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:error];
}

 

Guess you like

Origin blog.csdn.net/enuola/article/details/103200143