iphone - 在 iPhone 中记录来自蓝牙耳机的输入

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

我想我遗漏了一些需要在这里添加的东西。我只想要蓝牙耳机输入音频。任何帮助将不胜感激。

提前致谢!!

最佳答案

我刚刚回顾了你的问题并得到了很好的答案,你想用波纹管代码尝试一下:-

// 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

关于iphone - 在 iPhone 中记录来自蓝牙耳机的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18226255/

相关文章:

android - 多移动平台游戏开发(Android/iOS)

ios - 用于通用 rest API 的 NSData 对象中的 JSON

android - 如何在android中通过蓝牙发送媒体文件

android - ActivityManager : Warning: Activity not started, 它当前的任务已经被带到了前面

iphone - 以编程方式设置 UIBarButtonItem 的标识符

iphone - Xcode 4 和 iOS SDK 4.3 是否与 Snow Leopard 兼容?

iphone - 如何在objective-C中实现XMPP协议(protocol)?

iphone - UIImagePickerController imagePicker 不适合 iPhone 4 屏幕

ios - text becomeFirstResponder 抛出 EXC_BAD_ACCESS Code=1 Address=0x1

java - 通过蓝牙将数据从Arduino发送到Java程序