1

I am making an app that requires recording the voice coming from a Bluetooth headset and playing it on the iPhone speaker (not on Bluetooth headset speaker). I tried:

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;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
// 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);

After that I want to play the music file to iPhone speaker, and I used the code:

- (IBAction)playAudio:(id)sender {
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

    UInt32 size = sizeof(CFStringRef);
    CFStringRef route;
    OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
    NSLog(@"route = %@", route);


    // NSLog(@"route = %@", route);
    //[self playAudiokam];

    if (!_audioRecorder.recording)
    {
        NSError *error;

        _audioPlayer = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:_audioRecorder.url
                        error:&error];

        _audioPlayer.delegate = self;

        if (error)
            NSLog(@"Error: %@",
                  [error localizedDescription]);
        else
            [_audioPlayer play];
    }
}

Here my code is working, but not simultaneously. Apps require speaker continue playing music and recording the voice from Bluetooth headset.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81

0 Answers0