首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioEngine无声音

AVAudioEngine无声音
EN

Stack Overflow用户
提问于 2019-02-13 20:20:10
回答 1查看 840关注 0票数 1

我已经尝试了一段时间,现在如何从iOS中的URLSessionDataTask提供的数据中流式传输实时音频。

我声明了一个用于管理玩家操作的自定义类,它看起来如下所示:

代码语言:javascript
复制
import UIKit
import AVFoundation


class AudioDataPlayer: NSObject {

    //MARK:- Variables
    //MARK: Constants
    enum Status{
        case playing
        case notPlaying
    }

    let audioPlayerQueue = DispatchQueue(label: "audioPlayerQueue", qos: DispatchQoS.userInteractive)


    //MARK: Vars
    private (set) var currentStatus:Status = .notPlaying

    private var audioEngine: AVAudioEngine = AVAudioEngine()
    private var streamingAudioPlayerNode: AVAudioPlayerNode = AVAudioPlayerNode()
    private (set) var streamingAudioFormat: AVAudioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48000, channels: 2, interleaved: false)!



    //MARK:- Constructor
    override init() {
        super.init()

    }



    //MARK:- Private methods



    //MARK:- Public methods
    func processData(_ data:Data) throws{

        if currentStatus == .notPlaying{

            do{

                try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.allowAirPlay])
                try AVAudioSession.sharedInstance().setActive(true)

                if #available(iOS 11.0, *) {
                    try audioEngine.enableManualRenderingMode(.realtime, format: streamingAudioFormat, maximumFrameCount: 3072)
                }

                audioEngine.attach(streamingAudioPlayerNode)
                audioEngine.connect(streamingAudioPlayerNode, to: audioEngine.mainMixerNode, format: streamingAudioFormat)

                currentStatus = .playing

            }
            catch{

                print("\(logClassName) ERROR -> \(error.localizedDescription)")

            }

        }

        audioPlayerQueue.async {

            if let audioPCMBuffer = data.makePCMBuffer(format: self.streamingAudioFormat){

                self.streamingAudioPlayerNode.scheduleBuffer(audioPCMBuffer, completionHandler: {

                   //TODO
                })

                if !self.audioEngine.isRunning{

                    try! self.audioEngine.start()
                    self.streamingAudioPlayerNode.play()

                }

            }
            else{

                print("\(self.logClassName) TEST -> Ignoring data to play ...")

            }

        }

    }

    func stop(){

        audioEngine.stop()
        audioEngine.detach(streamingAudioPlayerNode)
        currentStatus = .notPlaying

    }


}

管理传入数据的函数是'processData(_ data: data )‘,它是这样从另一个类调用的:

代码语言:javascript
复制
let processingQueue = DispatchQueue(label: "processingQueue", qos: DispatchQoS.userInteractive)

var audioDataPlayer:AudioDataPlayer = AudioDataPlayer()

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {

    processingQueue.async {

        try! self.audioDataPlayer.processData(data)

    }

}

我从论坛和苹果文档网站上得到了代码。然而,也许我仍然不太明白它是如何工作的,没有声音从设备中传出……

音频数据为48K、16bit和2声道格式。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-14 01:11:32

如果你的音频数据是16位的(假设是整数),你需要用pcmFormatInt16而不是pcmFormatFloat32来初始化AVAudioFormat

而且对于这种格式来说,非交错似乎有点奇怪,所以您可能必须将interleaved设置为true

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54670067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档