Swift -从 audioPCMbuffer 到数组

标签: Xcode Swift
发布时间: 2017/1/22 20:43:32
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

它是可以提取缓冲区作为数组中的数据?我已经保存到缓冲区的 audiofile,现在会像 o 提取的数据。该代码是︰

import UIKit
import AVFoundation

//importing audio

let audioFileURL = Bundle.main.url(forResource: "Morning", withExtension: "wav")

let audioFile = try AVAudioFile(forReading: audioFileURL!)



//extract information

var audioFileFormat = audioFile.fileFormat

var audioFilePFormat = audioFile.processingFormat

var audioFileLength = audioFile.length

var audioFrameCount = UInt32(audioFile.length)

var audioFileChannels = audioFile.fileFormat.channelCount

var audioFileSamplingRate = audioFile.fileFormat.sampleRate



// insert into buffer

let audioBuffer = AVAudioPCMBuffer(pcmFormat: audioFilePFormat, frameCapacity: AVAudioFrameCount(audioFileLength)) 

try audioFile.read(into: audioBuffer, frameCount: AVAudioFrameCount(audioFileLength))

解决方法 1:

你可以这样做︰

let channelData = audioBuffer.floatChannelData![0]
let arr = Array(UnsafeBufferPointer(start:channelData, count: Int(audioFileLength)))
官方微信
官方QQ群
31647020