2015年10月21日 星期三

本篇文主要是實作 AVAudioSession bluetooth support, 你需要去看看 iOS audio API

http://devmonologue.com/ios/tutorials/avaudiosession-bluetooth-support/

AVAudioSession bluetooth support

本篇文主要是實作  AVAudioSession bluetooth support,
你需要去看看 iOS audio API


Adding bluetooth to an AVAudioSession involves several features as far as I’m concerned. Lets go through them all:

Enabling bluetooth audio
First of all, you need to specifically instruct AVAudioSession that you allow sound to be routed to bluetooth devices. This can be done while setting your audio category:

[session setCategory:AVAudioSessionCategoryPlayAndRecord 
         withOptions:AVAudioSessionCategoryOptionAllowBluetooth
               error:&error];


Remember, your audio session category tells iOS how you plan on using sound within your app. For instance, AVAudioSessionCategoryPlayAndRecord means that you want to both play and record audio. Anyway, what we are interested in, is the options parameter. By adding AVAudioSessionCategoryOptionAllowBluetooth, we allow iOS to play our app’s audio on a bluetooth audio device.

Note: I’ve seen projects where a category is set on an AVAudioSession in multiple places. If I’ve learned anything about audio in iOS is that you don’t use setCategory: lightly! In fact, in most cases you should only set is once. Why? For one thing it is a complex operation. If it’s executed on the main thread, it will make your UI unresponsive for a second. But more importantly, setting the category all over the place might lead into strange behavior – you will never know when audio will be going to the speaker or the earphone…

Switching between audio routes
When you are implementing AVAudioSession bluetooth support, you will probably want to be able to switch between audio devices. And there doesn’t seem to be a clearly defined way to do that in the documentation.

There are several ways to do it. However, what I found to be the best working one is setting the preferred audio input. Here are some examples:



沒有留言: