首页 > 解决方案 > 核心音频渲染回调中的时间戳可以为空吗?

问题描述

我有一个 iOS 锻炼应用程序,需要在后台保持运行(和播放音频)。可悲的是,据我所知,实现这一点的唯一方法是通过 Core Audio 播放听不见的声音。

一位用户正在经历我无法在模拟器中重现的重复崩溃。(他们有 iPhone 5、iOS 10。)Xcode 中的崩溃报告指出了我以前从未见过崩溃的行。在渲染回调中可以timestamp为 null 吗?

private func render(ctx: UnsafeMutableRawPointer,
                    ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
                    timeStamp: UnsafePointer<AudioTimeStamp>,
                    bus: UInt32,
                    nFrames: UInt32,
                    ioData: UnsafeMutablePointer<AudioBufferList>?) -> OSStatus
{
    guard let ioData = ioData else { return noErr }
    let abl = UnsafeMutableAudioBufferListPointer(ioData)
    guard let bufferData = abl[0].mData else { return noErr }

    let impl = Unmanaged<MyClass>.fromOpaque(ctx).takeUnretainedValue()

    let buf: UnsafeMutablePointer<Float32> = bufferData.assumingMemoryBound(to: Float32.self)
    // **** Crash appears to pointing to this next line. 
    var t: Int = Int(timeStamp.pointee.mSampleTime) % impl.samples.count
    for i in 0..<Int(nFrames) {
        buf[i] = impl.samples[t]
        t += 1
        if t >= impl.samples.count { t = 0 }
    }
    ...

标签: iosswift4core-audio

解决方案


推荐阅读