使用外设作为AVAudio输出

时间:2017-04-04 08:07:04

标签: ios swift bluetooth

将蓝牙LE外围设备(耳机)连接到我的设备后。 怎么用它来播放声音?

编辑:我想在外围设备上强制播放声音

实际上我正在使用此代码在设备扬声器上播放声音:

var engine = AVAudioEngine()
var player = AVAudioPlayerNode()
var pitch = AVAudioUnitTimePitch()

override func viewDidLoad() {
    super.viewDidLoad()
    player.volume = 1.0


    let path = NSBundle.mainBundle().pathForResource("Test", ofType: "m4a")!
    let url = NSURL.fileURLWithPath(path)

    let file = try? AVAudioFile(forReading: url)
    let buffer = AVAudioPCMBuffer(PCMFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))
    do {
        try file!.readIntoBuffer(buffer)
    } catch _ {
    }


    engine.attachNode(player)
    engine.attachNode(pitch)

    engine.connect(player, to: pitch, format: buffer.format)

    engine.connect(pitch, to: engine.mainMixerNode, format: buffer.format)
    player.scheduleBuffer(buffer, atTime: nil, options: AVAudioPlayerNodeBufferOptions.Loops, completionHandler: nil)


    engine.prepare()
    do {
        try engine.start()
    } catch _ {
    }

}

0 个答案:

没有答案