记录来自iPhone蓝牙耳机的输入

时间:2013-08-14 08:03:59

标签: iphone ios bluetooth

我有一个项目,我必须录制来自蓝牙耳机的声音并使用默认的iPhone扬声器播放。我搜索了很多并得到了这段代码。

UInt32 allowBluetoothInput = 1;

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

------------音频录音机启动和停止代码------------

- (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];
    }
}

我在此之后使用avaudiorecorder。在这里似乎还有其他一些东西。

--------录音机代码---------

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];
    }

我想我错过了其他需要添加的东西。我只想要蓝牙耳机输入音频。任何帮助将不胜感激。

先谢谢!!

3 个答案:

答案 0 :(得分:3)

我只是回顾你的问题并得到了你想要尝试使用Bellow代码的好答案: -

// 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];
    }

信用转到This

答案 1 :(得分:0)

将音频会话添加到您的代码

// create and set up the audio session
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    [audioSession setDelegate:self];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
    [audioSession setActive:YES error:nil];

答案 2 :(得分:0)

尝试这段惊人的代码。肯定会工作

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];
    }
}