首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在读AVAudioFile缓冲区上崩溃会快速创建波形

在读AVAudioFile缓冲区上崩溃会快速创建波形
EN

Stack Overflow用户
提问于 2019-09-05 06:40:49
回答 1查看 1K关注 0票数 2

我一直在AVAudioPlayer工作,并掀起了波澜。我正在使用AudioArmada Github在AudioArmada Pods的帮助下绘制波浪。但在某些情况下,AVAudioFile会在读取缓冲时间时崩溃。

代码如下:

代码语言:javascript
复制
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. 运行案例: 保护让文件= Bundle.main.url(forResource:"Beat9",withExtension:".mp3")或{返回}openFile(文件) AVAudioPCMBuffer输出: AVAudioPCMBuffer@0x6000005c9300: 0/35864064字节

波屏截图:

  1. 坠机案: Bundle.main.url(forResource:"IshqMubarak",withExtension:".mp3") withExtension{返回}openFile(文件) AVAudioPCMBuffer输出: AVAudioPCMBuffer@0x600003eb1180: 0/0字节/i,为什么要显示零字节缓冲区?

错误:致命错误:“尝试!”表达式意外引发一个错误: Foundation._GenericObjCError.nilError: file Foundation._GenericObjCError.nilError第63行

问题:如何解决这个问题,为什么有时会崩溃?

任何帮助都将不胜感激。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-12 23:53:50

看到这个错误,它会在WaveformZoomable.swift of AudioArmada的第63行崩溃,在该行中,try!被用于零对象。

AVAudioFile抛出(第56行相同):

代码语言:javascript
复制
func read(into buffer: AVAudioPCMBuffer) throws

您可以使用

  • try?暂时摆脱崩溃
  • 或者try catch找出它无法阅读的原因
  • 并在AudioArmada上提交一个问题
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57799737

复制
相关文章

相似问题

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