Playback through a bluetooth connected speaker

浪子不回头ぞ 提交于 2019-12-12 18:23:35

问题


In my app I am using the play and record category aka:

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
CheckError( AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
                                     sizeof (sessionCategory),
                                     &sessionCategory), "Couldn't set audio category");    

In the app any audio that plays would initially output through the receiver until I set this:

UInt32 doChangeDefaultRoute = 1;        
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);  

What I actually want however is for audio to output through a connected bluetooth speaker. For example I have a speaker that I connect with via bluetooth. I am able to play music through it with the native music app. However in my app when using the playandrecord category it only plays on the device. I have tried:

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

As well as:

CFStringRef audioRouteOverride = kAudioSessionOutputRoute_BluetoothHFP;

OSStatus s = AudioSessionSetProperty (kAudioSessionProperty_OutputDestination,
                                      sizeof(audioRouteOverride),&audioRouteOverride);

No luck. It seems as though this should be an easy property set but Idk. Any ideas?


回答1:


This is possible but your bluetooth device has to be headset or a bluetooth device with a built-in microphone. When you are in the playandrecord category it forces you to use the bluetooth mic as an input. If you do not have a bluetooth mic it won't play to bluetooth either (if you are recording at the same time). Also keep in mind when you are recording and streaming to BT you will only get 8 khz mono.




回答2:


So, first you need to know what kind of bluetooth device it is. Whether it is BluetoothHFP(input & output), BluetoothA2DP(output only) or BluetoothLE (output only). If the device is output only, you won't be able to connect and route the audio in kAudioSessionCategory_PlayAndRecord category. You could try setting the category to kAudioSessionCategory_PlayBack and see if this works.

I encountered the same problem a week ago. My application requires both input and output so I must set the category to kAudioSessionCategory_PlayAndRecord. So I plan to change the session category based on different type of bluetooth device.




回答3:


In case someone is also trying to figure this out it is not possible. Take a thorough look at all of the available audio session properties and you will find this to be the case




回答4:


I think you were over-thinking this. Simply setting the first part to allow bluetooth output and then ensuring that you're routing to none works.

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                         sizeof (audioRouteOverride),
                         &audioRouteOverride);

I was just testing this exact thing. Then in your route change listener, you should only have to worry about something else, like headphones being plugged in.



来源:https://stackoverflow.com/questions/10841065/playback-through-a-bluetooth-connected-speaker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!