Last active
December 11, 2021 04:11
-
-
Save kharmabum/10434424 to your computer and use it in GitHub Desktop.
Bluetooth audio session set up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App delegate | |
#import <AVFoundation/AVFoundation.h>// place in .h | |
// applicationDidFinishLaunchingWithOptions | |
// [self prepareAudioSession]; | |
- (BOOL)prepareAudioSession { | |
// deactivate session | |
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil]; | |
if (!success) { | |
NSLog(@"deactivationError"); | |
} | |
// set audio session category AVAudioSessionCategoryPlayAndRecord options AVAudioSessionCategoryOptionAllowBluetooth | |
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil]; | |
if (!success) { | |
NSLog(@"setCategoryError"); | |
} | |
// activate audio session | |
success = [[AVAudioSession sharedInstance] setActive:YES error: nil]; | |
if (!success) { | |
NSLog(@"activationError"); | |
} | |
return success; | |
} | |
// ViewController | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(myInterruptionSelector:) | |
name:AVAudioSessionInterruptionNotification | |
object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(myRouteChangeSelector:) | |
name:AVAudioSessionRouteChangeNotification | |
object:nil]; | |
} | |
return self; | |
} | |
- (void)listAvailableInputs | |
{ | |
// portDesc.portType could be for example - BluetoothHFP, MicrophoneBuiltIn, MicrophoneWired | |
NSArray *availInputs = [[AVAudioSession sharedInstance] availableInputs]; | |
int count = [availInputs count]; | |
for (int k = 0; k < count; k++) { | |
AVAudioSessionPortDescription *portDesc = [availInputs objectAtIndex:k]; | |
NSLog(@"input%i port type %@", k+1, portDesc.portType); | |
NSLog(@"input%i port name %@", k+1, portDesc.portName); | |
} | |
} | |
- (void)myRouteChangeSelector:(NSNotification*)notification | |
{ | |
AVAudioSessionRouteDescription *currentRoute = [[AVAudioSession sharedInstance] currentRoute]; | |
NSArray *inputsForRoute = currentRoute.inputs; | |
NSArray *outputsForRoute = currentRoute.outputs; | |
AVAudioSessionPortDescription *outPortDesc = [outputsForRoute objectAtIndex:0]; | |
NSLog(@"current outport type %@", outPortDesc.portType); | |
AVAudioSessionPortDescription *inPortDesc = [inputsForRoute objectAtIndex:0]; | |
NSLog(@"current inPort type %@", inPortDesc.portType); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment