iOS 紅外感應

weixin_33866037發表於2018-06-07

//
//  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


相關文章