iOS Question Headphone detect

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to test whether headphones are plugged in:

B4X:
Public Sub IsHeadsetPluggedIn As Boolean
   Dim no As NativeObject = Me
   Return no.RunMethod("isHeadsetPluggedIn", Null).AsBoolean
End Sub

#if objc
#import <AVFoundation/AVFoundation.h>
- (BOOL)isHeadsetPluggedIn {
  AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
  for (AVAudioSessionPortDescription* desc in [route outputs]) {
  if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
  return YES;
  }
  return NO;
}
#end if

B4X:
Log(IsHeadsetPluggedIn)
 
Upvote 0
Top