Mute other programs while playing audio, or play audio on top of other programs’ audio.

The AVAudioSession class is introduced by the AVFoundation framework. Every iOS app has an audio session. This session can be accessed by the sharedInstance class method of the AVAudioSession class as follows:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

Once you have an instance of the AVAudioSession class, you can choose from the different categories available to your iOS app by calling the setCategory: Error: instance method on the audio session object.

The categories of audio sessions available are listed below:

AVAudioSessionCategoryAmbient

This category doesn’t stop other apps’ sounds. Instead, it allows your audio to be played over other apps’ sounds, such as your iPod. Your application’s main UI thread will work fine. Both prepareToPlay and Play methods calling AVAPlayer return YES.

AVAudioSessionCategorySoloAmbient this very like AVAudioSessionCategoryAmbient category, in addition to the audio playback will stop other programs, such as the iPod. When the device is set to silent mode, your audio playback will stop.

AVAudioSessionCategoryRecord

This stops the sound of other apps (like iPod) and prevents your app from initializing audio playback (like AVAudioPlayer). In this mode, you can only record. With this class, calling AVAudioPlayer’s prepareToPlay returns YES, but calling the Play method returns NO. The main UI will work as usual. At this point, the app’s recording continues even if your device’s screen is locked by the user.

AVAudioSessionCategoryPlayback

This category disables audio playback for other apps (such as iPod apps). You can play sounds in your app using the prepareToPlay and Play methods of AVAudioPlayer. The main UI will work as usual. At this point, audio playback continues even if the screen is locked or the device is in silent mode.

AVAudioSessionCategoryPlayAndRecord

This category allows you to play and record sounds simultaneously in your application. When recording or playing your voice starts, other apps will stop playing your voice. The main UI will work as usual. At this point, audio playback and recording continue even if the screen is locked or the device is in silent mode.

AVAudioSessionCategoryAudioProcessing the categories used in the application of audio processing situation, instead of audio playback or recording. With this mode, you can’t play or record any sounds in the app. Both prepareToPlay and Play methods calling AVAPlayer return NO. Audio playback in other apps, such as the iPod, also stops in this mode.

Solution: When the application we developed is playing, suspend the playing of other applications, and when the application we developed is finished, continue the playing of other applications, the core code is as follows:

// Continue playing background music, Deactivate the current application of audio session + (void) resumeBackgroundSoundWithError: (NSError * *) error {[[AVAudioSession sharedInstance]setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:error]; } // Pause background music, To activate the current application of audio + (void) pauseBackgroundSoundWithError: (NSError * *) error {AVAudioSession * session = [AVAudioSession sharedInstance]; [sessionsetCategory:AVAudioSessionCategoryPlayback withOptions: AVAudioSessionCategoryOptionAllowBluetooth error:error];
    [session setActive:YES error:error];
}
  
+ (void)pauseBackgroundSoundWithCategoryRecord {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryRecord error:nil];
    [session setActive:YES error:nil];
}
Copy the code

Note:

1. The choice of the Category, depending on the activation of the application of audio session, the next step to recording or playback, if the recording, be sure to set the Category to AVAudioSessionCategoryRecord

1. If it is played, then set the AVAudioSessionCategoryPlayback

3. If ready to record, and set up become AVAudioSessionCategoryPlayback, may be an Unexpected result Unexpected error or warning