选择一个Category
#pragma mark -- Values for the category property --
/* Use this category for background sounds such as rain, car engine noise, etc.
Mixes with other music. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;
/* Use this category for background sounds. Other music will stop playing. */
AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;
/* Use this category for music tracks.*/
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;
/* Use this category when recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;
/* Use this category when recording and playing back audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;
/* Use this category when using a hardware codec or signal processor while
not playing or recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAudioProcessing;
AVAudioSessionCategoryAmbient 或 kAudioSessionCategory_AmbientSound
——用于非以语音为主的应用,使用这个category的应用会随着静音键和屏幕关闭而静音。并且不会中止其它应用播放声音,可以和其它自带应用如iPod,safari等同时播放声音。注意:该Category无法在后台播放声音
AVAudioSessionCategorySoloAmbient 或 kAudioSessionCategory_SoloAmbientSound
——类似于AVAudioSessionCategoryAmbient 不同之处在于它会中止其它应用播放声音。这个category为默认category。该Category无法在后台播放声音
AVAudioSessionCategoryPlayback或 kAudioSessionCategory_MediaPlayback
——用于以语音为主的应用,使用这个category的应用不会随着静音键和屏幕关闭而静音。可在后台播放声音
AVAudioSessionCategoryRecord 或 kAudioSessionCategory_RecordAudio
———用于需要录音的应用,设置该category后,除了来电铃声,闹钟或日历提醒之外的其它系统声音都不会被播放。该Category只提供单纯录音功能。
AVAudioSessionCategoryPlayAndRecord 或 kAudioSessionCategory_PlayAndRecord
——用于既需要播放声音又需要录音的应用,语音聊天应用(如微信)应该使用这个category。该Category提供录音和播放功能。如果你的应用需要用到iPhone上的听筒,该category是你唯一的选择,在该Category下声音的默认出口为听筒(在没有外接设备的情况下)。
设置Category
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
error: &setCategoryError];
if (!success) { /* handle the error in setCategoryError */ }
Activate & Deactivate AudioSession
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]);
}
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)
{
}
Audio Route的选择
当你的iPhone接有多个外接音频设备时(耳塞,蓝牙耳机等),AudioSession将遵循last-in wins的原则来选择外接设备,即声音将被导向最后接入的设备。
当没有接入任何音频设备时,一般情况下声音会默认从扬声器出来,但有一个例外的情况:在PlayAndRecord这个category下,听筒会成为默认的输出设备。如果你想要改变这个行为,可以提供MPVolumeView来让用户切换到扬声器,也可通过overrideOutputAudioPort方法来programmingly切换到扬声器,也可以修改category option为AVAudioSessionCategoryOptionDefaultToSpeaker。
PlayandRecord下切换到扬声器
除了让用户手动选择,你也可以通过以下两种方法在程序里进行切换
1. 修改Category的默认行为:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
2. OverrideOutputAudioPort:
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
这两种方法的区别官方文档中有详细的说明
在iOS7.0之前可用下面方法重定向(属于AudioSession)
audio route属性有以下两个,一个是默认的听筒,另一个则是扬声器。
- enum {
- kAudioSessionOverrideAudioRoute_None = 0,
- kAudioSessionOverrideAudioRoute_Speaker = 'spkr'
- };
<span style="font-family: Arial; white-space: pre;">Override audio route的方法有如下两种:</span>
- UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
- AudioSessionSetProperty (
- kAudioSessionProperty_OverrideAudioRoute
- sizeof (audioRouteOverride),
- &audioRouteOverride
- );
- UInt32 doChangeDefaultRoute = 1;
- AudioSessionSetProperty (
- kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
- sizeof (doChangeDefaultRoute),
- &doChangeDefaultRoute
- );