1

我已经尝试过用于 记录来自 iPhone 中的蓝牙耳机的输入的代码, 而使用这些代码我可以录制声音,但不能从蓝牙设备麦克风录制。它从设备(iphone)麦克风获取,我将如何将录制的声音路由到扬声器。请帮助我,任何帮助表示赞赏

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

1 回答 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];

// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);

并且当您想播放当时不在蓝牙设备扬声器中的声音时,会用新创建的会话覆盖会话

- (IBAction)playAudio:(id)sender {



[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);




UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
     //here u can diffrent route set up based on session audiosessionid

}

于 2013-08-23T05:03:48.883 回答