how to route iPhone audio to the bluetooth headset

前端 未结 2 1458
说谎
说谎 2020-12-01 03:42

I\'m attempting to output audio to the bluetooth headset (not A2DP) using AVAudioPlayer, AVAudioSession and AudioSessionSetProperty.

There seems to be functions to s

相关标签:
2条回答
  • 2020-12-01 04:20

    This little test worked for me... it involves setting up the bluetooth headset as the input also (not sure if that's what you want). Sorry about the crappy formatting on the code...

    // create and set up the audio session
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    [audioSession setDelegate:self];
    [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
    [audioSession setActive: YES error: nil];
    
    // set up for bluetooth microphone input
    UInt32 allowBluetoothInput = 1;
    OSStatus stat = AudioSessionSetProperty (
                             kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                             sizeof (allowBluetoothInput),
                             &allowBluetoothInput
                            );
    NSLog(@"status = %x", stat);    // problem if this is not zero
    
    // check the audio route
    UInt32 size = sizeof(CFStringRef);
    CFStringRef route;
    OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
    NSLog(@"route = %@", route);    
    // if bluetooth headset connected, should be "HeadsetBT"
    // if not connected, will be "ReceiverAndMicrophone"
    
    // now, play a quick sound we put in the bundle (bomb.wav)
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef        soundFileURLRef;
    SystemSoundID   soundFileObject;
    soundFileURLRef  = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL);
    AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
    AudioServicesPlaySystemSound (soundFileObject);     // should play into headset
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-01 04:22

    I was able to get this working, but it took some doing. I've pieced together the pertinent piece of code here:

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
    
    UInt32 allowBluetoothInput = 1;
    AudioSessionSetProperty(
        kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
        sizeof (allowBluetoothInput),
        &allowBluetoothInput);
    

    It is also possible to which sources are available and switch between bluetooth, a headset, handset or the speakerphone but things become very involved at that point. The audio source manager I ultimately wrote was over 700 lines.

    0 讨论(0)
提交回复
热议问题