【发布时间】:2019-09-05 06:40:49
【问题描述】:
我一直在 AVAudioPlayer 工作并画一个波浪。我正在使用AudioArmada Github pod 在AudioArmada Pods 的帮助下绘制波浪。但是某些情况下 AVAudioFile Crash on reading buffer time.
代码如下:
public func openFile(_ file: URL) {
let audioFile = try! AVAudioFile(forReading: file)
// specify the format we WANT for the buffer
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: audioFile.fileFormat.sampleRate, channels: audioFile.fileFormat.channelCount, interleaved: false)
// initialize and fill the buffer
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: UInt32(audioFile.length))
try! audioFile.read(into: buffer!)
// copy buffer to readFile struct
readFile.floatValuesLeft = Array(UnsafeBufferPointer(start: buffer?.floatChannelData?[0], count:Int(buffer!.frameLength)))
readFile.populated = true
reload(zoomFactor: zoomFactor)
}
public func reload(zoomFactor: Float = 1.0) {
self.zoomFactor = zoomFactor
setNeedsDisplay()
}
func drawSoundcloudWaveform(_ rect: CGRect) {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0.0, y: rect.height/2))
var index = 0
while index < readFile.leftPoints.count {
let point = readFile.leftPoints[index]
let drawFrom = CGPoint(x: point.x, y: path.currentPoint.y)
path.move(to: drawFrom)
let drawPointBottom = CGPoint(x: point.x, y: path.currentPoint.y + (point.y))
path.addLine(to: drawPointBottom)
path.addLine(to: CGPoint(x: drawPointBottom.x + pixelWidth, y: drawPointBottom.y))
path.addLine(to: CGPoint(x: drawFrom.x + pixelWidth, y: drawFrom.y))
path.close()
path.move(to: drawFrom)
let drawPointTop = CGPoint(x: point.x, y: path.currentPoint.y - (point.y))
path.addLine(to: drawPointTop)
path.addLine(to: CGPoint(x: drawPointTop.x + pixelWidth, y: drawPointTop.y))
path.addLine(to: CGPoint(x: drawFrom.x + pixelWidth, y: drawFrom.y))
path.close()
index = index + Int(pixelWidth) + Int(pixelSpacing)
}
UIColor(red:0.21, green:0.77, blue:0.78, alpha:1.0).set()
path.stroke()
path.fill()
}
以下情况:
-
运行案例:
guard let file = Bundle.main.url(forResource: "Beat9", withExtension: ".mp3") else { return } openFile(file) AVAudioPCMBuffer Output: AVAudioPCMBuffer@0x6000005c9300: 0/35864064 bytes
Wave 截图:
-
崩溃案例:
guard let file = Bundle.main.url(forResource: "IshqMubarak", withExtension: ".mp3") else { return } openFile(file) AVAudioPCMBuffer Output: AVAudioPCMBuffer@0x600003eb1180: 0/0 bytes // i m confused why show zero bytes buffer
错误: 致命错误:“尝试!”表达式意外引发错误:Foundation._GenericObjCError.nilError: file /Users/apple/Desktop/AudioArmada-90f214d3d9483e817cdb1396e08f8f626a7be821/AudioArmada/Classes/WaveformZoomable.swift,第 63 行
问题:如何解决这个问题以及为什么有时会崩溃?
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
-
我不是音频编程方面的专家,但是这行 - 'Foundation._GenericObjCError.nilError' - 让我认为您在访问文件时遇到了问题。文件不存在,或已损坏,或格式不可读;正在发生的事情使应用程序认为文件的长度为零,并且出现错误是因为您尝试缓冲不存在的数据。
-
好的@TedWrigley 我会检查的
标签: ios swift avaudiofile