1

我正在尝试从 PCMBuffer 播放音频。当我运行以下代码时,由于某种原因,没有播放音频。我没有收到任何错误。

let engine = AVAudioEngine()
let player = AVAudioPlayerNode()

let inputFormat = AVAudioFormat(
    commonFormat: .pcmFormatFloat32,
    sampleRate: 44100, channels: 2,
    interleaved: false)!
let audioData = decryptedPacket.data.toPCMBuffer(withFormat: inputFormat)

let mainMixer = engine.mainMixerNode
engine.attach(player)
engine.connect(player, to: mainMixer, format: inputFormat)
try! engine.start()
print("running: \(engine.isRunning)")

player.play()
player.scheduleBuffer(audioData) {
    print("audio buffer scheduled")
}

样本audioData值:

[-0.600482166, 0.979461014, -0.594195366, 0.229224518, 0.614184976, 0.0505996905, 0.89278847, 0.157444984, 0.380016476, -0.379222989, -0.986205637, -0.54072696, -0.104556412, -0.384929955, -0.491592139, -0.0335093252, 0.481429487, 0.248817414, 0.131748408, -0.829096317, -0.0964079723, 0.476912737, 0.958738983, -0.0563982055, -0.672078609, 0.380413234, 0.980986953, -0.870387912, -0.410107732, -0.359691143, 0.747917116, 0.993041754, 0.818781078, -0.114597, -0.418378234, -0.905636787, 0.458784759, 0.908688605, -0.984344006, -0.338023007, -0.0595721304, -0.33069858, 0.896115005, -0.00833155308, -0.72423476, -0.690847516, 0.149510175, 0.0758995339, -0.492355108, -0.860225201, -0.943967998, 0.114505447, -0.749229431, 0.289773256, -0.857417524, 0.35081026, 0.0593890212, 0.081240274, 0.563097, 0.697744668, -0.0673543513, -0.0834070891, -0.171117276, 0.30014953, 0.0271309558, 0.689352095, -0.0180669576, -0.843043327, -0.981414199, 0.934904039, -0.188390762, -0.679830313, -0.93841362, 0.0681783482, 0.559678972, 0.718863487, 0.429303885, 0.375255585, 0.620349765, -0.992950201, 0.122837, 0.441969067, -0.798242152, 0.30655843, 0.989226937, 0.0707113892, 0.325266272, -0.473036885, 0.506973505, -0.359111309, -0.34577471, -0.382610559, 0.618182957, -0.448072761, 0.128604993, -0.798242152, 0.0175176244, -0.13461715, -0.992156744, -0.0619220547, 0.660298467, 0.84539324, 0.937894821, 0.014618366, -0.953184605, -0.66377759, 0.172368541, 0.77086705, -0.704855502, -0.459486693, 0.121372111, -0.0715964213, 0.3002716, -0.0815759748, 0.738120675, -0.995513797, 0.524216413, -0.92370373, -0.506973505, 0.00753807183, 0.8800928, -0.946409523, -0.266609699, -0.187047943, -0.495864749, -0.416455567, -0.227759629, -0.200781271, 0.26532793, -0.148747221, 0.942655742, 0.549607813, -0.450544745, -0.675984979, 0.677144706, 0.179784536, -0.46644491, 0.0199285876, -0.477950364, 0.256935328, 0.919095457, 0.471327871, 0.951994359, 0.949827552, -0.169469282, 0.684347034, 0.461928159, 0.251655638, -0.872066379, 0.444227427, 0.485000163, -0.170384839, -0.947050393, -0.088259533, 0.604083359, -0.157506034, 0.582872987, 0.03869747, -0.650288403, -0.886196494, 0.463454098, -0.753410459, 0.293984801, -0.0406506546, -0.463026822, -0.205511644, 0.755058467, 0.380046993, 0.32969147, -0.293191314, -0.490768164, -0.953733921, -0.865871131, -0.10913419, -0.289651185, 0.0942411572, -0.217780083, -0.156895652, -0.610064983, -0.330393374, -0.168279067, -0.140354618, 0.752494872, 0.971892476, -0.105868712, -0.988250375, 0.434095293, -0.182164982, 0.73467207, -0.65559864, 0.677205741, -0.31284523, 0.38386181, -0.266121417, 0.412732333, -0.129795223, -0.384380639, -0.49623096, -0.71456039, 0.823389411, 0.717581689, 0.0607928708, 0.136295661, -0.755882442, -0.727958024, 0.117679372, -0.88750875, 0.486007273, -0.196722314, 0.512436271, -0.935209215, 0.415204316, -0.114932708, 0.145725876, -0.614764869, -0.153172404, -0.0797143504, 0.614276588, -0.730185866, 0.279061258, -0.290108949, 0.909482121, -0.704672396, -0.659352422, -0.00793481246, -0.136539817, -0.0372020639, -0.155735955, -0.126865447, -0.247871339, -0.19846186, -0.684408069, 0.439802229, 0.949583411, 0.315134138, 0.532639563, 0.918607116, -0.865047157, -0.847376943, -0.173039943, 0.0128482925, -0.954130709, -0.963530362, -0.992248297, -0.119663075]

为什么什么都没有播放?随时询问更具体的细节。

4

1 回答 1

1

你需要使用

engine.inputNode.outputFormat(forBus: 0)

而不是inputFormatengine.connect(...)方法中。

于 2018-08-20T05:00:43.523 回答