如何将 iPhone 音频路由到蓝牙耳机 [英] how to route iPhone audio to the bluetooth headset

查看:20
本文介绍了如何将 iPhone 音频路由到蓝牙耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to output audio to the bluetooth headset (not A2DP) using AVAudioPlayer, AVAudioSession and AudioSessionSetProperty.

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

There seems to be functions to select the bluetooth headset as input (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput), but no equivalent for setting the output. This is done in the Voicemail app, where you can select the headset, handset speaker or speaker phone. I've tried various combinations of SessionCategories and the AudioSession properties, but I just can't seem to hit on an approach that works.

我确定有人已经弄明白了,愿意分享一个例子吗?

I'm sure someone has figured this out, care to share an example?

推荐答案

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

This little test worked for me... it involves setting up the bluetooth headset as the input also (not sure if that's what you want). Sorry about the crappy formatting on the code...

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

希望有帮助!

这篇关于如何将 iPhone 音频路由到蓝牙耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆