iOS语音播放之切换听筒和扬声器
[[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;
{
if (isCloseToUser)
{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
} else {
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
if (!_isPlayingAudio) {
[[EMCDDeviceManager sharedInstance] disableProximitySensor];
}
}
[audioSession setActive:YES error:nil];
}
就没什么问题了。
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc]initWithTarget:self
action:@selector(longPressed:)];
[longPressGestureRecognizersetMinimumPressDuration:1.0f];
[longPressGestureRecognizersetAllowableMovement:50.0];
[self.bubbleBgImageViewaddGestureRecognizer:longPressGestureRecognizer];
[longPressGestureRecognizerrelease];
---------
-(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer
{
switch (gestureRecognizer.state)
{
caseUIGestureRecognizerStateEnded:
break;
caseUIGestureRecognizerStateCancelled:
break;
caseUIGestureRecognizerStateFailed:
break;
caseUIGestureRecognizerStateBegan:
if ([self.voiceDelegaterespondsToSelector:@selector(BaseChartVoiceLongPressed)])
{
[self.voiceDelegateBaseChartVoiceLongPressed];
}
break;
caseUIGestureRecognizerStateChanged:
break;
default:
break;
}
}
#pragma mark BaseChartCellDelegate
-(void)BaseChartVoiceLongPressed
{
NSLog(@"voice long Pressed");
if ([[[AVAudioSessionsharedInstance]category]isEqualToString:AVAudioSessionCategoryPlayback])
{
//切换为听筒播放
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[selfshowTipInfo:@"切换为听筒模式"];
}
else
{
//切换为扬声器播放
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:nil];
[selfshowTipInfo:@"切换为扬声器模式"];
}
}
注意 : 特此声明加入一个声音,播放的第三方下载地址:http://download.csdn.net/detail/wenhaiwang/8998967