[[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
proximityMonitoringEnabled 属性
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
proximityState 属性
传感器已启动前提条件下,如果用户接近 近距离传感器,此时属性值为YES,并且屏幕已关闭(非休眠)。And vice versa。
Notification
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:@"切换为扬声器模式"];
}
}