简体   繁体   English

在 AVAudioSession 中使用蓝牙播放

[英]Playing using bluetooth in AVAudioSession

I'm trying to play the voice using a bluetooth device using AVAudioSession.我正在尝试使用 AVAudioSession 使用蓝牙设备播放语音。 Here is my code which I've tried putting in both appdelegate.swift's didFinishLaunchingWithOptions and viewcontroller.swift (once at a time)这是我尝试放入 appdelegate.swift 的didFinishLaunchingWithOptions和 viewcontroller.swift 的代码(一次一次)

  import AVFoundation
var audioSession:AVAudioSession = AVAudioSession.sharedInstance() 

 audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.AllowBluetooth, error: nil)

So the code executes perfectly but does not use the connected bluetooth device.所以代码完美执行但不使用连接的蓝牙设备。 Actually, I opened control center on iPhone which says audio source as bluetooth device when app is not opened, but bluetooth device option disappears as soon as app is opened...实际上,我在 iPhone 上打开了控制中心,当应用程序没有打开时,它说音频源为蓝牙设备,但是一旦应用程序打开,蓝牙设备选项就会消失......

The rest of code(if thats of any use) is-其余的代码(如果有任何用处)是-

var engine = AVAudioEngine() 
var input = engine.inputNode 
var output = engine.outputNode 
var format = input.inputFormatForBus(0) 
var error:NSError? 
var audioSession:AVAudioSession = AVAudioSession.sharedInstance()    engine.startAndReturnError(&error)

It basically is a live audio player, which takes voice from mic and plays it to AUX, speaker, (bluetooth)它基本上是一个实时音频播放器,它从麦克风中获取声音并将其播放到 AUX、扬声器(蓝牙)

You are right, setting option AllowBluetooth is one and only way to turn on bluetooth in your application.您是对的,设置选项 AllowBluetooth 是在您的应用程序中打开蓝牙的一种也是唯一的方法。 But there could be a few problems with bluetooth:但是蓝牙可能存在一些问题:

  1. Your bluetooth device does not support protocol A2DP (Advanced Audio Distribution Profile), you can check it by playing music through standard Music app.您的蓝牙设备不支持协议 A2DP(高级音频分发配置文件),您可以通过标准音乐应用程序播放音乐来检查它。
  2. If you setup preferredBufferDuration or prefferedSampleRate it also may affect bluetooth.如果您设置preferredBufferDurationprefferedSampleRate,它也可能会影响蓝牙。

Perhaps the problem lies in this.或许问题就在于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM