美文网首页
iOS 红外感应

iOS 红外感应

作者: 曾柏超 | 来源:发表于2018-06-07 13:56 被阅读23次

//
//  ViewController.m
//  REDLine
//
//  Created by lzy on 2018/6/7.
//  Copyright © 2018 zbc. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
    
 
}


#pragma mark - 监听是否靠近耳朵
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *sessionError;
    if ([[UIDevice currentDevice] proximityState] == YES)
    {
        //靠近耳朵
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
    }
    else
    {
        //远离耳朵
        [session setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


相关文章

网友评论

      本文标题:iOS 红外感应

      本文链接:https://www.haomeiwen.com/subject/orlgsftx.html