我正在尝试使用 AVAudioPlayer、AVAudioSession 和 AudioSessionSetProperty 将音频输出到蓝牙耳机(不是 A2DP)。
似乎有选择蓝牙耳机作为输入的函数(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput),但没有用于设置输出的等效函数。这是在语音邮件应用程序中完成的,您可以在其中选择耳机、听筒扬声器或扬声器电话。我尝试了 SessionCategories 和 AudioSession 属性的各种组合,但我似乎找不到一种有效的方法。
我确信有人已经弄清楚了这一点,愿意分享一个例子吗?
这个小测试对我有用......它还涉及将蓝牙耳机设置为输入(不确定这是否是您想要的)。抱歉,代码的格式很糟糕......
// 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
希望有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)