【问题标题】:how to route iPhone audio to the bluetooth headset如何将 iPhone 音频路由到蓝牙耳机
【发布时间】:2011-01-23 11:12:57
【问题描述】:

我正在尝试使用 AVAudioPlayer、AVAudioSession 和 AudioSessionSetProperty 将音频输出到蓝牙耳机(不是 A2DP)。

似乎有选择蓝牙耳机作为输入的功能(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput),但没有设置输出的等效功能。这是在语音邮件应用程序中完成的,您可以在其中选择耳机、听筒扬声器或免提电话。我尝试了 SessionCategories 和 AudioSession 属性的各种组合,但我似乎无法找到一种可行的方法。

我确定有人已经解决了这个问题,愿意分享一个例子吗?

【问题讨论】:

  • 似乎没有人解决过这个问题(至少到目前为止还没有人在谈论这个问题!)我已经打开了一个关于这个主题的 Apple 支持票,当我有一个时会发布回复。
  • 是的,我做到了。套用一句,“不,你不能那样做。”我认为工程师并没有真正理解我的要求,因为我能够完成它。我会看看是否可以发布相关部分作为答案。

标签: iphone bluetooth


【解决方案1】:

这个小测试对我有用...它还涉及将蓝牙耳机设置为输入(不确定这是否是您想要的)。抱歉,代码格式很糟糕...

// 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

希望有帮助!

【讨论】:

  • 这看起来像我结束的代码,抱歉我没有早点来这里标记它;)
  • "文档有误。它影响输入和输出。"来自苹果 coreaudio 邮件列表 (lists.apple.com/archives/coreaudio-api/2009/Oct/msg00030.html)
  • AudioSessionSetProperty 自 iOS7 以来已弃用。如果不使用 AudioSessionSetProperty 会是什么样子?
  • 由于 AudioSessionSetProperty 无法播放。它的解决方案是什么?
【解决方案2】:

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

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

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

也可以使用哪些信号源并在蓝牙、耳机、听筒或免提电话之间切换,但此时事情变得非常复杂。我最终编写的音频源管理器有 700 多行。

【讨论】:

  • 我非常有兴趣了解如何在可用资源之间进行切换,您认为您可以公开该代码或在您的个人资料中添加一些信息以便我可以联系您吗?
猜你喜欢
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
相关资源
最近更新 更多