【发布时间】:2016-01-27 19:17:37
【问题描述】:
我想使用设备上的内置麦克风进行输入,使用蓝牙耳机进行输出。我的会话类别是并且必须是AVAudioSessionCategoryPlayAndRecord。我曾尝试将setPreferredInput: 用于内置麦克风,但它也会将输出切换到设备的扬声器。
我见过this(特别是 cmets),他们提到这是可能的。
【问题讨论】:
标签: ios objective-c avfoundation
我想使用设备上的内置麦克风进行输入,使用蓝牙耳机进行输出。我的会话类别是并且必须是AVAudioSessionCategoryPlayAndRecord。我曾尝试将setPreferredInput: 用于内置麦克风,但它也会将输出切换到设备的扬声器。
我见过this(特别是 cmets),他们提到这是可能的。
【问题讨论】:
标签: ios objective-c avfoundation
您可以使用此代码
// 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);
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlaySystemSound (soundFileObject);
【讨论】: