Select a category
AVAudioSessionCategoryAmbient
OrkAudioSessionCategory_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
AVAudioSessionCategorySoloAmbient
OrkAudioSessionCategory_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
AVAudioSessionCategoryPlayback
OrkAudioSessionCategory_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
AVAudioSessionCategoryRecord
OrkAudioSessionCategory_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.
AVAudioSessionCategoryPlayAndRecord
OrkAudioSessionCategory_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
- Nserror *setcategoryerror = nil;
- BOOL success = [[Avaudiosession sharedinstance]
- Setcategory:avaudiosessioncategoryambient
- Error: &setCategoryError];
- if (!success) {/* Handle the error in Setcategoryerror */}
Activate & Deactivate Audiosession
[OBJC]View Plaincopy
- Nserror *error = nil;
- Avaudiosession *audiosession = [Avaudiosession sharedinstance];
- BOOL ret = [audiosession setactive:yes error:&error];
- if (!ret)
- {
- NSLog (@ "%s-activate Audio session failed with error%@", __func__,[error description]);
- }
[OBJC]View Plaincopy
- Nserror *error = nil;
- Avaudiosession *audiosession = [Avaudiosession sharedinstance];
- Note:set Avaudiosessionsetactiveoptionnotifyothersondeactivation to resume other apps ' audio.
- BOOL ret = [audiosession setactive:no withoptions:avaudiosessionsetactiveoptionnotifyothersondeactivation Error: &error];
- if (!ret)
- {
- }
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
- [Audiosession Setcategory:avaudiosessioncategoryplayandrecord withoptions: Avaudiosessioncategoryoptiondefaulttospeaker error:&error];
2. Overrideoutputaudioport:
[OBJC]View Plaincopy
- [Audiosession Overrideoutputaudioport:avaudiosessionportoverridespeaker error:&error];
Audiosession Detailed category Select handset Speaker switch