藍牙連接,CoreBluetooth通過代碼控制的僅僅是對于數(shù)據(jù)的傳遞,但是并不能處理對于藍牙音頻的判斷谣妻。
相對的在對于音頻播報軟件來說楞黄,當與藍牙配對之后点把,尤其是導航類軟件類似高德導航荞胡,在長時間的連接與息屏的情況下个扰,會出現(xiàn)藍牙音頻切換到手機音頻播報,但是藍牙音頻卻又未斷開萎津,導致聲音很小卸伞,這種狀態(tài)下體驗會很差。
AVAudioSessionRouteChangeReason changeReason = [dict[AVAudioSessionRouteChangeReasonKey] intValue];
switch (changeReason) {
case AVAudioSessionRouteChangeReasonUnknown:
break;
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
break;
case AVAudioSessionRouteChangeReasonOverride:
break;
case AVAudioSessionRouteChangeReasonWakeFromSleep:
break;
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
break;
case AVAudioSessionRouteChangeReasonRouteConfigurationChange:
break;
}
通過上面的AVFoundation 監(jiān)聽機制锉屈,可以判斷:手機音頻的切換原因
AVAudioSessionRouteDescription *routeDescription = dict[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription? *portDescription? = [routeDescription.outputs firstObject];
NSString *portType = portDescription.portType;
再次判斷類型:得出手機音頻切換之前的音頻類型:BluetoothA2DPOutput荤傲、BluetoothHFP、Speaker颈渊、Receiver...
而我所需要的 僅僅是 藍牙音頻的機制: 因此可以進行判斷:
BOOL flag1 = ?[portType isEqualToString:@"BluetoothA2DPOutput"];
BOOL flag2 = ?[portType isEqualToString:@"BluetoothHFP"];
BOOL flag3 = ?changeReason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable;
if ((flag1 ||flag2) && flag3){
/*之前的設(shè)備是藍牙 ?在這里面進行音頻播報的通道設(shè)置遂黍, 重新切換音頻通道到藍牙
設(shè)定bool值 终佛,在音頻播報的情況下 ,修改音頻category*/
}
下面 我需要切換播報的category:
typedef NS_OPTIONS(NSUInteger, AVAudioSessionCategoryOptions){
/* MixWithOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and? AVAudioSessionCategoryMultiRoute */
AVAudioSessionCategoryOptionMixWithOthers = 0x1,
/* DuckOthers is only valid with AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
AVAudioSessionCategoryOptionDuckOthers = 0x2,
/* AllowBluetooth is only valid with AVAudioSessionCategoryRecord and AVAudioSessionCategoryPlayAndRecord */
AVAudioSessionCategoryOptionAllowBluetooth __TVOS_PROHIBITED __WATCHOS_PROHIBITED = 0x4,
/* DefaultToSpeaker is only valid with AVAudioSessionCategoryPlayAndRecord */
AVAudioSessionCategoryOptionDefaultToSpeaker __TVOS_PROHIBITED __WATCHOS_PROHIBITED = 0x8,
/* InterruptSpokenAudioAndMixWithOthers is only valid with AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategoryPlayback, and AVAudioSessionCategoryMultiRoute */
AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers NS_AVAILABLE_IOS(9_0) = 0x11,
/* AllowBluetoothA2DP is only valid with AVAudioSessionCategoryPlayAndRecord */
AVAudioSessionCategoryOptionAllowBluetoothA2DP API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0)) = 0x20,
/* AllowAirPlay is only valid with AVAudioSessionCategoryPlayAndRecord */
AVAudioSessionCategoryOptionAllowAirPlay API_AVAILABLE(ios(10.0), tvos(10.0)) __WATCHOS_PROHIBITED = 0x40,
} NS_AVAILABLE_IOS(6_0);
上面代碼中,蘋果api設(shè)定:AVAudioSessionCategoryOptionAllowBluetooth 需AVAudioSessionCategoryPlayAndRecord模式才可以起作用雾家,因此 我們需要重新設(shè)定option:
AVAudioSession *session =?[AVAudioSession sharedInstance] ;
[session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth|
AVAudioSessionCategoryOptionAllowBluetoothA2DP|AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
error:nil];
這樣就可以在每次播報的時候強制扭轉(zhuǎn)音頻到藍牙铃彰,但我感覺這并不是最友好的方式,希望有人提出更加優(yōu)化的方式芯咧。