gpt4 book ai didi

ios - 读取 AVAudioFile 缓冲区时崩溃创建 Waves Swift

转载 作者:行者123 更新时间:2023-12-02 11:25:16 37 4
gpt4 key购买 nike

我一直在AVAudioPlayer中工作并画一个波浪。我正在使用AudioArmadaAudioArmada Pods 的帮助下,Github pods 绘制波浪。但某些情况下 AVAudioFile 在读取缓冲时间时崩溃。

代码如下:

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()
}

以下情况:

  1. 运行案例:

    guard let file = Bundle.main.url(forResource: "Beat9", withExtension: ".mp3") else { return }
    openFile(file)

    AVAudioPCMBuffer Output: AVAudioPCMBuffer@0x6000005c9300: 0/35864064 bytes

波浪屏幕截图:

Running Case Screen Shot

  • 崩溃案例:

    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
  • Error: Fatal error: 'try!' expression unexpectedly raised an error: Foundation._GenericObjCError.nilError: file /Users/apple/Desktop/AudioArmada-90f214d3d9483e817cdb1396e08f8f626a7be821/AudioArmada/Classes/WaveformZoomable.swift, line 63

    问题:如何解决此问题以及为什么有时会崩溃?

    任何帮助将不胜感激。

    提前致谢。

    最佳答案

    AudioArmada 的 WaveformZoomable.swift 第 63 行看到崩溃的错误,其中 try! 用于 nil 对象。 p>

    AVAudioFile抛出(与第 56 行相同):

    func read(into buffer: AVAudioPCMBuffer) throws
    <小时/>

    您可以使用

    • 尝试一下?暂时摆脱崩溃
    • try catch查找无法读取的原因
    • 并在 AudioArmada 上提交问题

    关于ios - 读取 AVAudioFile 缓冲区时崩溃创建 Waves Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57799737/

    37 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com