iOS更换带有接近传感器的设备扬声器

时间:2019-02-27 19:11:45

标签: ios objective-c avplayer proximitysensor

我有一个播放器,它从云摄像机流式传输视频和音频。

我正在尝试配置音频输出。音频从两个扬声器(耳朵和下方)发出。现在...

但是我不希望它在传感器启动时通过下面的扬声器发出

[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[notificationCenter addObserver:self selector:@selector(sensorStateMonitor:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];

 _audioSession = [AVAudioSession sharedInstance];
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[_audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

- (void)sensorStateMonitor:(NSNotificationCenter *)notification
{
    if ([[UIDevice currentDevice] proximityState] == YES){
        NSLog(@"Device is close to user.");
        [_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
    }
    else{
        [_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
        NSLog(@"Device is not closer to user.");
    }
    [_audioSession setActive:YES error:nil];
NSLog(@"Current audio route: %@",_audioSession.currentRoute.outputs);

}

在激活传感器时,我有此日志

  

设备靠近用户。[avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:端口接收器的未知选定数据源(类型:接收器)    当前音频路由:(AVAudioSessionPortDescription:0x2813d8020,类型=接收器;名称=接收器; UID =内置接收器; selectedDataSource =(空)   )

当传感器关闭此日志时

  

设备离用户不近。    [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:端口Bocina的未知选定数据源(类型:扬声器)   当前音频路由:(AVAudioSessionPortDescription:0x2813d2620,类型=扬声器;名称= Bocina; UID =扬声器; selectedDataSource =(null)>“   )

但是声音总是通过两个扬声器发出的

0 个答案:

没有答案