首页 > 代码库 > iOS语音播放之切换听筒和扬声器的方法解决方案
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;
{
}
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:@"切换为扬声器模式"];
}
}
iOS语音播放之切换听筒和扬声器的方法解决方案