record input coming from bluetooth headset in iPhone

后端 未结 3 2171
长情又很酷
长情又很酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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

提交回复
热议问题