iOS 传感器(随手记)
感兴趣的看官可以通过这篇文章http://www.jianshu.com/p/fb03e978bf87了解iPhone内置传感器
传感器的应用
距离传感器应用(Proximity sensor直译)
//AudioPlayer之通过距离传感器切换听筒和扬声器
- (void)addSensorObserver
{
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)removeSensorObserver
{
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];//不需要时,最好关闭
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)sensorStateChange:(NSNotification *)notice;
{
//NSError *error;
//AVAudioSession *session = [AVAudioSession sharedInstance];
//如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗省电并防止耳朵触摸屏幕
if ([[UIDevice currentDevice] proximityState] == YES)
{
NSLog(@"贴近耳朵");
//[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
}
else
{
NSLog(@"远离耳朵");
//[session setCategory:AVAudioSessionCategoryPlayback error:&error];
}
/*
if(error)
{
NSLog(@"Error creating session: %@",error.description);
}
else
{
[session setActive:YES error:nil];
}
*/
}