8

I have a project in which I have to record the voice coming from bluetooth headset and play with default iPhone speaker. I have searched a lot and got this code.

UInt32 allowBluetoothInput = 1;

    AudioSessionSetProperty (
                             kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                             sizeof (allowBluetoothInput),
                             &allowBluetoothInput
                             );

------------ CODE FOR AUDIO RECORDER START AND STOP ------------

- (IBAction)Record: (id)sender
{
    UIButton *btn = (UIButton *)sender;
    if([btn isSelected])
    {
        [audioRecorder stop];
        [btn setSelected:NO];
        [btn setTitle:@"Start Recording" forState:UIControlStateNormal];
    }
    else
    {
        [audioRecorder record];
        [btn setSelected:YES];
        [btn setTitle:@"Stop Recording" forState:UIControlStateNormal];
    }
}

and i am using avaudiorecorder after this. There seems to be something else I am missing in here.

-------- Code for audio recorder ---------

NSURL *soundFileURL = [NSURL fileURLWithPath:AUDIO_FILE];

    NSDictionary *recordSettings = [NSDictionary
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [audioRecorder prepareToRecord];
    }

I think i am missing something else which needs to be added here. I just want the bluetooth headset input audio. Any help would be appreciated.

Thanks in Advance!!

Swati
  • 1,417
  • 1
  • 15
  • 35

3 Answers3

4

i just review your issue and got nice answer something you want try with Bellow 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);

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURLRef
                     settings:recordSettings
                     error:&error];
    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [audioRecorder prepareToRecord];
    }

Credit goes to This

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • this really worked. Thanks.. But the audio output when i play the recording also goes to bluetooth device. I want it in device speaker. Can this be done? – Swati Aug 14 '13 at 08:38
  • Add this two line and maight be Helps AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject); AudioServicesPlaySystemSound (soundFileObject); – Nitin Gohel Aug 14 '13 at 08:42
  • where exactly should i add these two lines? – Swati Aug 14 '13 at 08:45
  • after this soundFileURLRef = CFBundleCopyResourceURL – Nitin Gohel Aug 14 '13 at 08:46
  • this method of yours did not work for me but i have added the code to change the property to audiospeaker while playing. Thanks a lot. – Swati Aug 14 '13 at 09:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35417/discussion-between-nitin-gohel-and-swati) – Nitin Gohel Aug 14 '13 at 09:58
  • @Swati : Can you add your changes here ? So, future visitors can get proper guidance. – Bhavin Aug 15 '13 at 10:10
  • is there any way i can block my dvice mic input? – Swati Aug 16 '13 at 11:49
  • hi @Swati check this:-http://stackoverflow.com/questions/287543/how-to-programmatically-sense-the-iphone-mute-switch – Nitin Gohel Aug 16 '13 at 11:50
  • this link talks about detecting if the speaker is mute or not. I want to block my device mic. So that when a person is speaking through bluetooth, only that sound gets recorded and not the device mic voice. – Swati Aug 18 '13 at 12:55
  • swati can i get code to record voice from BT and to paly on device speaker any appriciated.i have same requirement,even i had used the above code but this code not record the voice from BT but it play sound in BT device – kamalesh kumar yadav Aug 22 '13 at 09:51
  • Hey Swati, I want same thing to do.the audio output when i play the recording also goes to bluetooth device. I want it in device speaker. Please provide me some solution or code lines. Thanks – Dhaval Aug 28 '13 at 10:00
  • Should be noted that although certainly correct when posted, AudioSessionSetProperty was deprecated in iOS7. If you're building for iOS7+, you want to instead call AVSession setCategory: withOptions: and pass AVAudioSessionCategoryOptionAllowBluetooth as the options argument – Reid May 28 '14 at 21:45
0

add the audio session to your code

// create and set up the audio session
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    [audioSession setDelegate:self];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
    [audioSession setActive:YES error:nil];
Agent Chocks.
  • 1,312
  • 8
  • 19
0

Try out this amazing piece of code. Will work for sure

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth  error:NULL];
[audioSession setActive:YES error:NULL];

NSString *preferredPortType = AVAudioSessionPortBluetoothHFP;
for (AVAudioSessionPortDescription *desc in audioSession.availableInputs)
{
    if ([desc.portType isEqualToString: preferredPortType])
    {
        [audioSession setPreferredInput:desc error:nil];
    }
}
Kalpesh Panchasara
  • 1,730
  • 2
  • 15
  • 27