1
在我的應用程序,我有錄製音頻和播放音頻,並添加接近時,播放音頻您的耳朵附近所以爲了這個,我不得不這樣做代碼時播放按鈕點擊:如何禁用接近時AVPlayer停止播放
- (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
}
通知調用此方法時接近實現:
- (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];
}
}
當播放器停止播放或暫停我補充一點:
UIDevice *device = [UIDevice currentDevice];
[device setProximityMonitoringEnabled: NO];
但是當我再次開始播放音頻並把耳機放在靠近耳朵的那個時候,它叫做通知方法(proximityChanged),當時接近狀態也得到1,音頻會話類別也設置爲播放和記錄,但是它沒有在耳邊揚聲器中播放這個音頻。它在主揚聲器中播放音頻。
請在這幫助我。
先謝謝您。