Swift 3 LPCM 录音机 |错误:kAudioFileInvalidPacketOffsetError

2024-04-09

下面的录音机仅在第一次时有效,如果您尝试第二次录音,则在尝试 AudioFileWritePackets 时会出现错误“kAudioFileInvalidPacketOffsetError”。

知道为什么会发生这种情况吗?

先感谢您

存储库位于此处 https://github.com/LuAndreCast/iOS_AudioRecordersAndPlayers/tree/master/recorder_LPCM

Recorder

import UIKit
import CoreAudio
import AudioToolbox


class SpeechRecorder: NSObject {

    static let sharedInstance = SpeechRecorder()

    // MARK:- properties
    @objc enum Status: Int {
        case ready
        case busy
        case error
    }

    internal struct RecordState {
        var format: AudioStreamBasicDescription
        var queue: UnsafeMutablePointer<AudioQueueRef?>
        var buffers: [AudioQueueBufferRef?]
        var file: AudioFileID?
        var currentPacket: Int64
        var recording: Bool
    };

    private var recordState: RecordState?

    var format: AudioFormatID {
        get { return recordState!.format.mFormatID }
        set {  recordState!.format.mFormatID = newValue }
    }

    var sampleRate: Float64 {
        get { return recordState!.format.mSampleRate }
        set {  recordState!.format.mSampleRate = newValue  }
    }

    var formatFlags: AudioFormatFlags {
        get {  return recordState!.format.mFormatFlags }
        set {   recordState!.format.mFormatFlags = newValue  }
    }

    var channelsPerFrame: UInt32 {
        get {   return recordState!.format.mChannelsPerFrame }
        set {   recordState!.format.mChannelsPerFrame = newValue }
    }

    var bitsPerChannel: UInt32 {
        get {   return recordState!.format.mBitsPerChannel }
        set {   recordState!.format.mBitsPerChannel = newValue  }
    }

    var framesPerPacket: UInt32 {
        get {  return recordState!.format.mFramesPerPacket }
        set {   recordState!.format.mFramesPerPacket = newValue }
    }

    var bytesPerFrame: UInt32 {
        get {  return recordState!.format.mBytesPerFrame }
        set {   recordState!.format.mBytesPerFrame = newValue }
    }

    var bytesPerPacket: UInt32 {
        get { return recordState!.format.mBytesPerPacket  }
        set {  recordState!.format.mBytesPerPacket = newValue }
    }

    //MARK: - Handlers
    public var handler: ((Status) -> Void)?

    // MARK:- Init
    override init()
    {
        super.init()
        self.recordState = RecordState(format: AudioStreamBasicDescription(),
                                       queue: UnsafeMutablePointer<AudioQueueRef?>.allocate(capacity: 1),
                                       buffers: [AudioQueueBufferRef?](repeating: nil, count: 1),
                                       file: nil,
                                       currentPacket: 0,
                                       recording: false)
    }//eom



    // MARK:- OutputFile
    func setOutputFile(path: String)
    {
        setOutputFile(url: URL(fileURLWithPath: path))
    }

    func setOutputFile(url: URL)
    {
        AudioFileCreateWithURL(url as CFURL,
                               kAudioFileWAVEType,
                               &recordState!.format,
                               AudioFileFlags.dontPageAlignAudioData.union(.eraseFile),
                               &recordState!.file)
    }

    // MARK:- Start / Stop Recording
    func start()
    {
        handler?(.busy)

        let inputAudioQueue: AudioQueueInputCallback =
            { (userData: UnsafeMutableRawPointer?,
                audioQueue: AudioQueueRef,
                bufferQueue: AudioQueueBufferRef,
                startTime: UnsafePointer<AudioTimeStamp>,
                packets: UInt32,
                packetDescription: UnsafePointer<AudioStreamPacketDescription>?) in

                let internalRSP = unsafeBitCast(userData, to: UnsafeMutablePointer<RecordState>.self)
                if packets > 0
                {
                    var packetsReceived = packets
                    let outputStream:OSStatus = AudioFileWritePackets(internalRSP.pointee.file!,
                                                                      false,
                                                                      bufferQueue.pointee.mAudioDataByteSize,
                                                                      packetDescription,
                                                                      internalRSP.pointee.currentPacket,
                                                                      &packetsReceived,
                                                                      bufferQueue.pointee.mAudioData)
                    if outputStream != 0
                    {
                        // This is where the error occurs when recording after the first time
                        //<----DEBUG
                        switch outputStream
                        {
                            case kAudioFilePermissionsError:
                                print("kAudioFilePermissionsError")
                                break
                            case kAudioFileNotOptimizedError:
                                print("kAudioFileNotOptimizedError")
                                break
                            case kAudioFileInvalidChunkError:
                                print("kAudioFileInvalidChunkError")
                                break
                            case kAudioFileDoesNotAllow64BitDataSizeError:
                               print("kAudioFileDoesNotAllow64BitDataSizeError")
                                break
                            case kAudioFileInvalidPacketOffsetError:
                                print("kAudioFileInvalidPacketOffsetError")
                                break
                            case kAudioFileInvalidFileError:
                                print("kAudioFileInvalidFileError")
                                break
                            case kAudioFileOperationNotSupportedError:
                                print("kAudioFileOperationNotSupportedError")
                                break
                            case kAudioFileNotOpenError:
                                print("kAudioFileNotOpenError")
                                break
                            case kAudioFileEndOfFileError:
                                print("kAudioFileEndOfFileError")
                                break
                            case kAudioFilePositionError:
                                print("kAudioFilePositionError")
                                break
                            case kAudioFileFileNotFoundError:
                                print("kAudioFileFileNotFoundError")
                                break
                            case kAudioFileUnspecifiedError:
                                print("kAudioFileUnspecifiedError")
                                break
                            case kAudioFileUnsupportedFileTypeError:
                                print("kAudioFileUnsupportedFileTypeError")
                                break
                            case kAudioFileUnsupportedDataFormatError:
                                print("kAudioFileUnsupportedDataFormatError")
                                break
                            case kAudioFileUnsupportedPropertyError:
                                print("kAudioFileUnsupportedPropertyError")
                                break
                            case kAudioFileBadPropertySizeError:
                                print("kAudioFileBadPropertySizeError")
                                break
                            default:
                                print("unknown error")
                                break
                        }
                        //<----DEBUG
                    }
                    internalRSP.pointee.currentPacket += Int64(packetsReceived)
                }

                if internalRSP.pointee.recording
                {
                    let outputStream:OSStatus = AudioQueueEnqueueBuffer(audioQueue, bufferQueue, 0, nil)
                    if outputStream != 0
                    {
                     // This is where the error occurs when recording after the first time
                    //<----DEBUG
                    switch outputStream
                    {
                        case kAudioFilePermissionsError:
                            print("kAudioFilePermissionsError")
                            break
                        case kAudioFileNotOptimizedError:
                            print("kAudioFileNotOptimizedError")
                            break
                        case kAudioFileInvalidChunkError:
                            print("kAudioFileInvalidChunkError")
                            break
                        case kAudioFileDoesNotAllow64BitDataSizeError:
                            print("kAudioFileDoesNotAllow64BitDataSizeError")
                            break
                        case kAudioFileInvalidPacketOffsetError:
                            print("kAudioFileInvalidPacketOffsetError")
                            break
                        case kAudioFileInvalidFileError:
                            print("kAudioFileInvalidFileError")
                            break
                        case kAudioFileOperationNotSupportedError:
                            print("kAudioFileOperationNotSupportedError")
                            break
                        case kAudioFileNotOpenError:
                            print("kAudioFileNotOpenError")
                            break
                        case kAudioFileEndOfFileError:
                            print("kAudioFileEndOfFileError")
                            break
                        case kAudioFilePositionError:
                            print("kAudioFilePositionError")
                            break
                        case kAudioFileFileNotFoundError:
                            print("kAudioFileFileNotFoundError")
                            break
                        case kAudioFileUnspecifiedError:
                            print("kAudioFileUnspecifiedError")
                            break
                        case kAudioFileUnsupportedFileTypeError:
                            print("kAudioFileUnsupportedFileTypeError")
                            break
                        case kAudioFileUnsupportedDataFormatError:
                            print("kAudioFileUnsupportedDataFormatError")
                            break
                        case kAudioFileUnsupportedPropertyError:
                            print("kAudioFileUnsupportedPropertyError")
                            break
                        case kAudioFileBadPropertySizeError:
                            print("kAudioFileBadPropertySizeError")
                            break
                        default:
                            print("unknown error")
                            break
                    }
                    //<----DEBUG
                }
            }
    }

    let queueResults = AudioQueueNewInput(&recordState!.format, inputAudioQueue, &recordState, nil, nil, 0, recordState!.queue)
    if queueResults == 0
    {
        let bufferByteSize: Int = calculate(format: recordState!.format, seconds: 0.5)
        for index in (0..<recordState!.buffers.count)
        {
            AudioQueueAllocateBuffer(recordState!.queue.pointee!, UInt32(bufferByteSize), &recordState!.buffers[index])
            AudioQueueEnqueueBuffer(recordState!.queue.pointee!, recordState!.buffers[index]!, 0, nil)
        }

        AudioQueueStart(recordState!.queue.pointee!, nil)
        recordState?.recording = true
    }
    else
    {
        print("Error setting audio input.")
        handler?(.error)
    }
}//eom

func stop()
{
    recordState?.recording = false
    if let recordingState: RecordState = recordState
    {
        AudioQueueStop(recordingState.queue.pointee!, true)
        AudioQueueDispose(recordingState.queue.pointee!, true)
        AudioFileClose(recordingState.file!)

        handler?(.ready)
    }
}//eom

// MARK:- Helper methods
func calculate(format: AudioStreamBasicDescription, seconds: Double) -> Int
{
    let framesRequiredForBufferTime = Int(ceil(seconds * format.mSampleRate))
    if framesRequiredForBufferTime > 0

    {
        return (framesRequiredForBufferTime * Int(format.mBytesPerFrame))
    }
    else
    {
        var maximumPacketSize = UInt32(0)
        if format.mBytesPerPacket > 0
        {
            maximumPacketSize = format.mBytesPerPacket
        }
        else
        {
            audioQueueProperty(propertyId: kAudioQueueProperty_MaximumOutputPacketSize, value: &maximumPacketSize)
        }

        var packets = 0
        if format.mFramesPerPacket > 0
        {
            packets = (framesRequiredForBufferTime / Int(format.mFramesPerPacket))
        } else
        {
            packets = framesRequiredForBufferTime
        }

        if packets == 0
        {
            packets = 1
        }

        return (packets * Int(maximumPacketSize))
    }
}//eom

func audioQueueProperty<T>(propertyId: AudioQueuePropertyID, value: inout T)
{
    let propertySize = UnsafeMutablePointer<UInt32>.allocate(capacity: 1)
    propertySize.pointee = UInt32(MemoryLayout<T>.size)

    let queueResults = AudioQueueGetProperty(recordState!.queue.pointee!, propertyId, &value, propertySize)
    propertySize.deallocate(capacity: 1)

    if queueResults != 0 {
        print("Unable to get audio queue property.")
    }
    }//eom
  }

视图控制器

import UIKit

import AudioToolbox

class ViewController: UIViewController {

    //MARK: - Properties
    var recorder:SpeechRecorder?

    @IBOutlet weak var startStopRecordingButton: UIButton!



    //MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

       //having same recorder gives error
       recorder = SpeechRecorder()
    }


    //MARK: - Start / End Recording

    func startRecording()
    {
        //alloc/init recorder everytime we start recording gives no error
        //recorder = SpeechRecorder()


        //settings
        recorder?.format = kAudioFormatLinearPCM
        recorder?.sampleRate = 16000;
        recorder?.channelsPerFrame = 1
        recorder?.bitsPerChannel = 16
        recorder?.framesPerPacket = 1
        recorder?.bytesPerFrame = ((recorder!.channelsPerFrame * recorder!.bitsPerChannel) / 8)
        recorder?.bytesPerPacket = recorder!.bytesPerFrame * recorder!.framesPerPacket
        recorder?.formatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked

        //outputfile
        let outputfilePath:String = MyFileManager().createTempFilePathWithUniqueName("recorderAudio", andExtension: "wav")
        print("temp filepath: ", outputfilePath)
        recorder?.setOutputFile(path: outputfilePath)


        //handler
        recorder?.handler = { [weak self] status in
            switch status
            {
                case .busy:
                    print("started Recording\n\n")
                    break
                case .ready:
                    print("finish recorder, ready to start recording\n\n")
                    break
                case .error:
                    print("error occur with recorder\n\n")

                    DispatchQueue.main.async
                    {
                        self?.startStopRecordingButton.isSelected = false
                        self?.view.backgroundColor = UIColor.white
                    }

                    break
                }
        }//


        recorder?.start()
    }//eom


    func stopRecording()
    {
      recorder?.stop()
    }//eom

    //MARK: - Actions
    @IBAction func startStopRecording()
    {
        if startStopRecordingButton.isSelected
        {
            startStopRecordingButton.isSelected = false
            self.view.backgroundColor = UIColor.white
            startStopRecordingButton.setTitle("Start Recording", for: UIControlState.normal)

            self.stopRecording()
        }
        else
        {
            startStopRecordingButton.isSelected = true
            self.view.backgroundColor = UIColor.green
            startStopRecordingButton.setTitle("Stop Recording", for: UIControlState.normal)

            self.startRecording()
        }
    }//eom



    //MARK: - Memory
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

文件管理器(创建临时文件路径)

import Foundation

@objc class MyFileManager:NSObject
{
     private let unique_debug = true
     private var _temporyDirectory:String = ""

    //MARK: - Properties
    var directory:String {
        return _temporyDirectory
    }

    //MARK: - Init
    override init() {
        super.init()

        _temporyDirectory = NSTemporaryDirectory()
    }//eom

    func createHomeDirFileUniqueWithName(_ myFileName:String, andExtension fileExtension:String)->URL
    {
        //filename
        let time:Date = Date.init()
        let dateformatter:DateFormatter = DateFormatter()
        dateformatter .dateFormat = "ddMMyyyy-hh-mm-ss-a"
        let tempDate:String = dateformatter .string(from: time)
        let tempFileName = "\(myFileName)-\(tempDate).\(fileExtension)"

        //directory
        var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

        documentsDirectory.appendPathComponent(tempFileName)

        if unique_debug {  print("\(documentsDirectory)") }

        return documentsDirectory
    }//eom

    //MARK: - Names
    func createGlobalUniqueFileName(_ myFileName:String)->String
    {
        let guid = ProcessInfo.processInfo.globallyUniqueString
        let uniqueFileName = ("\(myFileName)_\(guid)")

        if unique_debug {  print("\(uniqueFileName)") }

        return uniqueFileName
    }//eom

    func createUniqueNameWithFilename(_ myFileName:String, andExtension fileExtension:String)->String
    {
        //filename
        let time:Date = Date.init()
        let dateformatter:DateFormatter = DateFormatter()
        dateformatter .dateFormat = "ddMMyyyy-hh-mm-ss-a"
        let currentDateString = dateformatter .string(from: time)

        let finalName = myFileName + currentDateString + "." + fileExtension

        if unique_debug {  print("\(finalName)") }

        return finalName
    }//eom

    //MARK: - Paths
    func createTempFilePathWithUniqueName(_ myFileName:String, andExtension fileExtension:String)->String
    {
        let tempFileName = self.createUniqueNameWithFilename(myFileName, andExtension: fileExtension)

        let tempFile = _temporyDirectory + tempFileName

        if unique_debug {  print("\(tempFile)") }

        return tempFile
    }//eom

    //MARK: - Helpers
    func enumerateDirectory(directory:String)
    {
        do
        {
            let filesInDir:[String] = try FileManager.default.contentsOfDirectory(atPath: directory)
            for currFile in filesInDir {
                print(currFile)
            }//eofl
        }
        catch let error
        {
            print("error: \(error.localizedDescription)")
        }
    }//eom

    func doesFileExistInDirectory(filename:String) -> Bool {
        do
        {
            let filesInDir:[String] = try FileManager.default.contentsOfDirectory(atPath: _temporyDirectory)
            for currFile in filesInDir
            {
                print(currFile)
                if currFile == filename {
                    return true
                }
            }//eofl
        }
        catch let error
        {
            print("error: \(error.localizedDescription)")
        }

        return false
    }//eom

}//eoc

你没有重置你的currentPacket数到零,所以在后续录音中,你会问AudioFileWritePackets以非零起始数据包开始写入新文件,但它拒绝这样做。

正确的解决方案是(可能)重新创建RecordState每次你开始录音时,尽管设置很简单

recordState!.currentPacket = 0

打电话之前AudioQueueNewInput似乎也有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift 3 LPCM 录音机 |错误:kAudioFileInvalidPacketOffsetError 的相关文章

随机推荐

  • 在设备驱动程序中使用 select()/poll()

    我有一个驱动程序 它可以处理多个 TCP 连接 有没有一种方法可以在给定列表的情况下执行类似于内核中用户空间应用程序 api 的 select poll epoll 的操作struct sock s Thanks 您可能想编写自己的自定义s
  • MATLAB 类对象未更新

    我正在编写一个简单的 MATLAB 类 它具有一些属性和一个方法 类的构造函数使用默认值初始化属性 类的方法在类被构造之后获得额外的输入以更新类属性 classdef classTest properties p1 p2 p3 p4 end
  • 如何使用 css 代码单独设置 gtk 小部件的样式

    在这之后GTK 将字体更改为旋转按钮 https stackoverflow com questions 47083294 gtk change font to spin button和这个为什么 CSS 样式没有应用于 GTK 代码 ht
  • 如何使用文本操作

    使用目的是什么文本动作 http docs oracle com javase 6 docs api javax swing text TextAction html from 抽象动作 http docs oracle com javas
  • 将cpp文件添加到cocos2d-x项目android项目中

    我已按照本教程进行操作http www raywenderlich com 33750 cocos2d x tutorial for ios and android getting started http www raywenderlic
  • Facebook Graph API:如何获取评论中的“来自”字段

    我有一个尚未发布的 Facebook 应用程序 测试模式 我使用页面访问令牌从我自己页面上特定帖子的评论中提取 来自 字段 但它返回空字段 这是我的图形 API 查询 gt post id comments fields from 当我使用
  • 给单元格着色 Google Chart - 散点图

    我在我的一个项目中使用谷歌图表 我需要使用以下代码为谷歌散点图中的一组单元格着色 我在用google visualization arrayToDataTable用于处理数据 以下是我的代码 div div
  • 在 Android 中使用 Service 作为单例

    创建一个不好的做法吗 Service作为单身人士工作 我的意思是一个Service它永远不会停止 并且包含一些其他引擎和Activities会使用 所以Service可能有类似的东西 public class CustomService e
  • 对空间数据使用简单的 for 循环

    抱歉 这将是一个 for 循环 101 问题 我正在努力编写一个简单的 for 循环来根据经度纬度数据生成城市之间的距离表 locations lt read csv distances csv Locations 返回下表 City Ty
  • 具有自由 CORS 政策的公开托管图像? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在做一些将图像加载到画布上的测试 并且正在使用私下里在我们的 aws cdn 上托管图像 这个 c
  • 如何找到 ROI 并检测内部标记?

    我是计算机视觉的初学者 我有一个关于检测和跟踪的问题 我想检测下图中的白色矩形 以确定感兴趣的区域并检测红色标记的轮廓 但我不想利用颜色信息来检测标记 谁能给我关于如何做到这一点的建议 如果您只想检测圆圈 则可以使用经过调整的霍夫变换 ht
  • 具有有序索引的 R 向量-向量匹配

    这里我有两个字符串向量 它们的顺序很重要并且无法更改 vec1 lt c carrot carrot carrot apple apple mango mango cherry cherry vec2 lt c cherry apple 我
  • 可通过属性名称或索引选项访问的结构

    我对 Python 非常陌生 并试图弄清楚如何创建一个具有可通过属性名称或索引访问的值的对象 例如 os stat 返回 stat result 或 pwd getpwnam 返回 struct passwd 的方式 在试图弄清楚这一点时
  • alloca可以完全替代吗?

    我读过很多地方alloca已过时 不应使用 而应使用可变长度数组 我的问题是这样的 是alloca完全可以用变长数组代替 在我的特定实例中 我有一些看起来像这样的东西 typedef struct int value size t size
  • 如何在 Kotlin 中编写以下代码来实现回调

    我如何像java一样用Kotlin编写 Callback callback new Callback Override public void getCallback ServerResponse serverResponse var ca
  • 基于 RCP 的应用程序的 P2 更新失败

    我尝试通过 P2 更新站点更新基于 Eclipse RCP 3 5 的应用程序 该应用程序包含两个功能 产品是由Eclipse Buckminster P2 更新站点的创建是产品构建的一部分 当通过菜单开始更新时 Update gt Che
  • 为什么这个未使用的 self.hash 方法会导致“无法将字符串转换为整数”错误?

    我正在跑过Lynda Rails 3 教程 http www lynda com Ruby on Rails 3 tutorials essential training 55960 2 html 在某一时刻 在名为 access cont
  • 如何检测重复数据?

    我有一个简单的联系人数据库 但用户输入重复数据时遇到问题 我已经实现了一个简单的数据比较 但不幸的是 输入的重复数据并不完全相同 例如 姓名拼写错误 或者一个人输入 Bill Smith 另一个人输入 William Smith 表示同一个
  • 使用 $(function 等启动 javascript 代码

    我正在研究 Backbone 和来自的待办事项示例应用程序http todomvc com http todomvc com 我注意到有 3 种方法可以在文件中启动代码 function code here function code he
  • Swift 3 LPCM 录音机 |错误:kAudioFileInvalidPacketOffsetError

    下面的录音机仅在第一次时有效 如果您尝试第二次录音 则在尝试 AudioFileWritePackets 时会出现错误 kAudioFileInvalidPacketOffsetError 知道为什么会发生这种情况吗 先感谢您 存储库位于此