如何将iPhone音频路由到蓝牙耳机

如何将iPhone音频路由到蓝牙耳机,iphone,bluetooth,Iphone,Bluetooth,我正在尝试使用AVAudioPlayer、AVAudioSession和AudioSessionSetProperty将音频输出到蓝牙耳机(不是A2DP) 似乎有选择蓝牙耳机作为输入的功能(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput),但没有设置输出的等效功能。这是在语音信箱应用程序中完成的,您可以在其中选择耳机、手机扬声器或扬声器手机。我尝试了SessionCategories和AudioSession属性的各种组合,但

我正在尝试使用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

希望有帮助

我能让它工作,但需要一些努力。我在这里拼凑了相关的代码:

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(
    kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
    sizeof (allowBluetoothInput),
    &allowBluetoothInput);

也可以选择可用的源,并在蓝牙、耳机、手机或扬声器之间切换,但在这一点上,事情变得非常复杂。我最终编写的音频源管理器有700多行。

似乎还没有人解决这个问题(至少到目前为止还没有人讨论过!)我已经打开了一个苹果公司关于这个问题的支持票,当我有一个问题的时候,我会发布一个回复。是的,我做到了。换言之,“不,你不能那样做。”我不认为工程师真的理解我的要求,因为我能够做到。我会看看是否可以发布相关部分作为答案。这看起来也像是我最终得到的代码,很抱歉我没有早点来这里标记;)apple coreaudio邮件列表()中的AudioSessionSetProperty自iOS7以来已被弃用。如果不使用AudioSessionSetProperty,情况会如何?由于AudioSessionSetProperty,无法播放。什么是它的解决方案?我非常感兴趣的是看看如何在可用的源代码之间切换,你认为你可以公开该代码,或者在你的个人资料中添加一些信息,我可以与你联系吗?