【问题标题】:record input coming from bluetooth headset in iPhone在 iPhone 中记录来自蓝牙耳机的输入
【发布时间】:2013-08-14 08:03:58
【问题描述】:

我有一个项目,我必须录制来自蓝牙耳机的声音并使用默认的 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];
    }

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

提前致谢!!

【问题讨论】:

  • 你好@Swati 需要了解您已完成的功能。请在您在线时评论我,我需要知道它,所以请。
  • 今天有 hollyday swati 所以请在星期一发表评论。如果你上网时有胆量
  • 好的。您也在 IST 区域。我明天试试上网。

标签: iphone ios bluetooth


【解决方案1】:

我刚刚查看了您的问题并得到了很好的答案,您想尝试使用波纹管代码:-

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

【讨论】:

  • 这确实有效。谢谢..但是我播放录音时的音频输出也会进入蓝牙设备。我想要它在设备扬声器中。这个可以吗?
  • 添加这两行可能是 Helps AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject); AudioServicesPlaySystemSound (soundFileObject);
  • 这两行应该加在哪里?
  • 在此 soundFileURLRef = CFBundleCopyResourceURL 之后
  • 您的这种方法对我不起作用,但我添加了代码以在播放时将属性更改为 audiospeaker。非常感谢。
【解决方案2】:

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

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

【讨论】:

  • 在我玩的时候添加这个不会播放任何东西
  • 其余代码与您的代码相同,我刚刚向您发送了音频会话
【解决方案3】:

试试这段神奇的代码。肯定会工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多