iOS AVAudioSession、AudioSession Category选择和声音通道听筒、扬声器切换

来源:互联网 发布:高考逆袭 知乎 编辑:程序博客网 时间:2024/06/11 12:13

选择一个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_EXPORTNSString *const AVAudioSessionCategoryAmbient;

/*  Use this category for background sounds.  Other music will stop playing. */

AVF_EXPORTNSString *const AVAudioSessionCategorySoloAmbient;


/* Use this category for music tracks.*/

AVF_EXPORTNSString *const AVAudioSessionCategoryPlayback;


/*  Use this category when recording audio. */

AVF_EXPORTNSString *const AVAudioSessionCategoryRecord;


/*  Use this category when recording and playing back audio. */

AVF_EXPORTNSString *const AVAudioSessionCategoryPlayAndRecord;


/*  Use this category when using a hardware codec or signal processor while

 not playing or recording audio. */

AVF_EXPORTNSString *const AVAudioSessionCategoryAudioProcessing;


AVAudioSessionCategoryAmbient  kAudioSessionCategory_AmbientSound

——用于非以语音为主的应用,使用这个category的应用会随着静音键和屏幕关闭而静音。并且不会中止其它应用播放声音,可以和其它自带应用如iPodsafari等同时播放声音。注意: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,程序应该根据实际需要来切换设置不同的category,举个例子,录音的时候,需要设置为AVAudioSessionCategoryRecord当录音结束时,应根据程序需要更改categoryAVAudioSessionCategoryAmbientAVAudioSessionCategorySoloAmbientAVAudioSessionCategoryPlayback中的一种。

设置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 optionAVAudioSessionCategoryOptionDefaultToSpeaker

 

PlayandRecord下切换到扬声器

除了让用户手动选择,你也可以通过以下两种方法在程序里进行切换

1. 修改Category的默认行为:

 

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];  

2. OverrideOutputAudioPort:

 

[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; 

这两种方法的区别方文档中有详细的说明


在iOS7.0之前可用下面方法重定向(属于AudioSession)

audio route属性有以下两个,一个是默认的听筒,另一个则是扬声器。

 

  1. enum {    
  2.    kAudioSessionOverrideAudioRoute_None    = 0,  
  3.    kAudioSessionOverrideAudioRoute_Speaker = 'spkr'  
  4. };  
<span style="font-family: Arial; white-space: pre;">Override audio route的方法有如下两种:</span>

 

  1. UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;  
  2.       
  3.     AudioSessionSetProperty (  
  4.                              kAudioSessionProperty_OverrideAudioRoute  
  5.                              sizeof (audioRouteOverride),  
  6.                              &audioRouteOverride  
  7.                              );  
  1. UInt32 doChangeDefaultRoute = 1;  
  2.       
  3.     AudioSessionSetProperty (  
  4.                              kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,  
  5.                              sizeof (doChangeDefaultRoute),  
  6.                              &doChangeDefaultRoute  
  7.                              );  

1 0
原创粉丝点击