在語音/通話類的 App 中,聲音需要在聽筒與揚聲器之間切換,這通常借助 iPhone 的距離感測器(近傳感器)來判斷。本文整理了完整的實作方式,並特別說明實戰中容易踩到的坑與對應解法。
播放前開啟紅外感應,並註冊近距離狀態變更通知:
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; // 建議播放前設為YES,播放結束設為NO
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:)
name:@"UIDeviceProximityStateDidChangeNotification"
object:nil];
處理監聽觸發事件——當手機靠近耳邊時輸出到聽筒,遠離時輸出到揚聲器:
-(void)sensorStateChange:(NSNotificationCenter *)notification {
// 手機靠近面部放在耳朵旁,聲音通過聽筒輸出,並將屏幕變暗(省電)
if ([[UIDevice currentDevice] proximityState] == YES) {
NSLog(@"Device is close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
} else {
NSLog(@"Device is not close to user");
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
初始化播放器時,預設以揚聲器播放:
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; // 默認情況下揚聲器播放 [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioSession setActive:YES error:nil];
UIDevice 中有兩個相關屬性:proximityMonitoringEnabled 與 proximityState,均需 iOS 3.0 以上。並非所有裝置都具備近距離感測器,因此添加監聽前應先嘗試啟用,若設定後仍為 NO,代表該裝置不支援:
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
移除監聽時也應先確認狀態,再關閉功能:
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
對於不希望常駐啟動近傳感器的應用,若在切換過程中處理不當,會出現一個問題:當聲音透過聽筒播放完畢、此時仍處於聽筒模式,如果直接關閉感測器功能,由於感測器已關閉,App 將無法再次收到狀態變更通知,導致聲音輸出模式無法切回,聲音仍從聽筒輸出。
更棘手的是不同機型的差異:在 iPhone 4s 及 iPhone 5 上,感測器關閉後若狀態為 YES,再次啟動時不會收到變更通知;而在 iPhone 4 上則會先收到一次通知。
解法:在「非黑屏狀態」下才關閉近傳感器功能。具體做法是——當感測器狀態為 YES 且播放已結束、卻遲遲未觸發狀態變更時,先不要關閉感測器;等到障礙物離開、收到變更通知後,再在其中檢測當前感測器狀態與播放狀態,若播放已結束則關閉感測器即可:
-(void)sensorStateChange:(NSNotificationCenter *)notification {
if ([[UIDevice currentDevice] proximityState] == YES) { // 黑屏
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
} else { // 沒黑屏
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
if (![MTool isPlayRecodering]) {
// 沒有播放了,也不在黑屏狀態下,就可以把距離傳感器關了
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
}
}
}
除了自動切換,也可以加上長按手勢,讓使用者手動在聽筒與揚聲器之間切換:
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)]; [longPressGestureRecognizer setMinimumPressDuration:1.0f]; [longPressGestureRecognizer setAllowableMovement:50.0]; [self.bubbleBgImageView addGestureRecognizer:longPressGestureRecognizer]; [longPressGestureRecognizer release];
-(void)BaseChartVoiceLongPressed {
if ([[[AVAudioSession sharedInstance] category] isEqualToString:AVAudioSessionCategoryPlayback]) {
// 切換為聽筒播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[self showTipInfo:@"切換為聽筒模式"];
} else {
// 切換為揚聲器播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[self showTipInfo:@"切換為揚聲器模式"];
}
}
掌握「非黑屏才關閉感測器」這個關鍵細節,再輔以手動切換,就能打造穩定可靠的語音播放體驗。