Search code examples
iosavplayerproximitysensor

how to disable proximity when AVPlayer stop playing


In my app i have to record audio and play this audio and add proximity when play this audio near your ear so for this i have to do this code when play button click:

- (IBAction)btnPlayPauseTapped:(id)sender {
    UIDevice *device = [UIDevice currentDevice];
    [device setProximityMonitoringEnabled: YES];
    if (device.proximityMonitoringEnabled == YES) {
        [[NSNotificationCenter defaultCenter] removeObserver:APP_DELEGATE name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:APP_DELEGATE selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
//other code 
    }

Notification call this method when proximity enable:

- (void) proximityChanged:(NSNotification *)notification {
    NSLog(@"proximity changed called");
    UIDevice *device = [notification object];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    if(device.proximityState == 1){
        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
        [audioSession setActive:YES error:nil];
    }
    else{
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [audioSession  overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
        [audioSession setActive:YES error:nil];

    }
}

when player stop playing or pause i add this:

UIDevice *device = [UIDevice currentDevice];
[device setProximityMonitoringEnabled: NO];

but when i again start playing audio and put device near ear that time it called notification method(proximityChanged) and at that time proximity state also get 1 and audio session category also set as playandrecord but it did not play this audio in ear speaker . it play audio in main speaker.

please help me in this.

Thank you in Advance.


Solution

  • I Solved my problem after 2 days in that when player stop player as I disable proximity is correct but I changed audiosession category in proximitychange method.

      - (void) proximityChanged:(NSNotification *)notification {
        UIDevice *device = [notification object];
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        BOOL success;
         success = [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        if(device.proximityState == 1){
            NSLog(@"bool %s", success ? "true" : "false");
            [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
            [audioSession setActive:YES error:nil];
        }
        else{
             NSLog(@"bool %s", success ? "true" : "false");
            [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
            [audioSession setActive:YES error:nil];
    
        }