我正在尝试使用AVAudioPlayer,AVAudioSession和AudioSessionSetProperty将音频输出到蓝牙耳机(而不是A2DP)。
似乎有选择蓝牙耳机作为输入的功能(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput),但没有相应的设置输出。这可以在语音信箱应用程序中完成,您可以在其中选择耳机,听筒扬声器或扬声器电话。我已经尝试过SessionCategories和AudioSession属性的各种组合,但我似乎无法找到一种有效的方法。
我确定有人已经想出了这个,小心分享一个例子?
答案 0 :(得分:16)
这个小测试对我有用......它还涉及设置蓝牙耳机作为输入(不确定这是否是你想要的)。对于代码中糟糕的格式很抱歉...
// 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); // should play into headset
希望有所帮助!
答案 1 :(得分:6)
我能够让这个工作,但它花了一些时间。我在这里拼凑了相关的代码:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput);
也可以使用哪些信号源并在蓝牙,耳机,听筒或扬声器之间切换,但事情变得非常复杂。我最终写的音频源管理器超过700行。