[[UIDevice currentDevice] setProximityMonitoringEn
//添加監聽
[[NSNotificationCenter defaultCenter] addObserver:self
//處理監聽觸發事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
}
//初始化播放器的時候如下設置
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
UInt32 audioRouteOverride
= kAudioSessionOverrideAud
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//默認情況下揚聲器播放
[audioSession setCategory:AVAudioSessionCategoryPl
[audioSession setActive:YES error:nil];
在 iOS 中,並非所有 iOS 設備都擁有近距離傳感器。這裡介紹如何調用 iPhone 的距離傳感器。
UIDevice
To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.
要確定近距離傳感器是否可用,可以嘗試啟用它,即 proximityMonitoringEnabl
傳感器已啟動前提條件下,如果用戶接近 近距離傳感器,此時屬性值為YES,並且屏幕已關閉(非休眠)。And vice versa。
UIDeviceProximityStateDi
#pragma mark - 處理近距離監聽觸發事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
}
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(longPressed:)];
[longPressGestureRecognizer setMinimumPressDuration:1.0f];
[longPressGestureRecognizer setAllowableMovement:50.0];
[self.bubbleBgImageView addGestureRecognizer:longPressGestureRecognizer];
[longPressGestureRecognizer release];
---------
-(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer
{
switch (gestureRecognizer.state)
{
case UIGestureRecognizerStateEnded:
break;
case UIGestureRecognizerStateCancelled:
break;
case UIGestureRecognizerStateFailed:
break;
case UIGestureRecognizerStateBegan:
if ([self.voiceDelegate respondsToSelector:@selector(BaseChartVoiceLongPressed)])
{
[self.voiceDelegate BaseChartVoiceLongPressed];
}
break;
case UIGestureRecognizerStateChanged:
break;
default:
break;
}
}
#pragma mark BaseChartCellDelegate
-(void)BaseChartVoiceLongPressed
{
NSLog(@"voice long Pressed");
if ([[[AVAudioSession sharedInstance] category] isEqualToString:AVAudioSessionCategoryPlayback])
{
//切換為聽筒播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[self showTipInfo:@"切換為聽筒模式"];
}
else
{
//切換為揚聲器播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[self showTipInfo:@"切換為揚聲器模式"];
}
}