record input coming from bluetooth headset in iPhone

后端 未结 3 2144
长情又很酷
长情又很酷 2020-12-09 13:09

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.

<         


        
相关标签:
3条回答
  • 2020-12-09 13:43

    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];
    
    0 讨论(0)
  • 2020-12-09 13:54

    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

    0 讨论(0)
  • 2020-12-09 14:00

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题