swift AVAudioFile init在模拟器上工作,但在设备上崩溃

u91tlkcl  于 2023-06-21  发布在  Swift
关注(0)|答案(2)|浏览(110)

我想构建一个iOS应用程序(使用Swift 3和Xcode 8.2.1),它可以从iPhone的麦克风中录音,并将记录保存到.caf文件中。
首先,我在项目的 Info.plist 文件中添加了以下行:

<key>NSMicrophoneUsageDescription</key>
<string>Some description to explain why access is required</string>

然后,我创建了一个简单的UIViewController,它设置了一个AVAudioEngine,并在点击RecordStop按钮时安装/删除音频抽头。

  • ViewController.swift*
import UIKit
import AVFoundation

class ViewController: UIViewController {

    var engine = AVAudioEngine()
    var file: AVAudioFile?
    var player = AVAudioPlayerNode()

    override func viewDidLoad() {
        super.viewDidLoad()

        guard let url = urlFor(filename: "test.caf") else { return }
        do {
            file = try AVAudioFile(forWriting: url, settings: engine.inputNode!.inputFormat(forBus: 0).settings, commonFormat: engine.inputNode!.inputFormat(forBus: 0).commonFormat, interleaved: false)
        } catch {
            print("Error: \(error)")
        }

        engine.attach(player)
        engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormat(forBus: 0))

        do {
            try engine.start()
        } catch {
            print("Error: \(error)")
        }
    }

    @IBAction func record(sender: AnyObject) {
        engine.inputNode?.installTap(onBus: 0, bufferSize: 1024, format: engine.mainMixerNode.outputFormat(forBus: 0)) { (buffer, time) -> Void in
            do {
                try self.file?.write(from: buffer)
            } catch {
                print("Error: \(error)")
            }
        }
    }

    @IBAction func stop(sender: AnyObject) {
        engine.inputNode?.removeTap(onBus: 0)
        // Print the url of the saved file
        let urls = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
        print(urls[urls.endIndex - 1])
    }

    func urlFor(filename: String) -> URL? {
        // Get url of saved file
        if let dir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first {
            let path = dir.appending(filename)
            return URL(fileURLWithPath: path)
        }
        return nil
    }

}

该应用程序在模拟器上运行良好:我可以录制我的声音,获取录制文件的url并测试我的声音是否已正确录制。然而,当我在设备上启动该应用程序时,它就会崩溃(在iOS 10.2.1上用iPhone 6s测试)。控制台提供以下日志:

2017-02-08 17:09:14.330151 AudioEngineSimpleApp[7100:2659416] Audio files cannot be non-interleaved. Ignoring setting AVLinearPCMIsNonInterleaved YES.
2017-02-08 17:09:14.333715 AudioEngineSimpleApp[7100:2659416] [central] 54:   ERROR:    [0x1b7440c40] >avae> AVAudioFile.mm:160: AVAudioFileImpl: error -54
2017-02-08 17:09:14.333796 AudioEngineSimpleApp[7100:2659416] [central] 54:   ERROR:    [0x1b7440c40] >avae> AVAudioFile.mm:122: ReadMagicCookie: error -50
2017-02-08 17:09:14.334369 AudioEngineSimpleApp[7100:2659416] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -50'

这个问题似乎与Stack Overflow线程AVAudioPlayer working on Simulator but not on Real Device有关,但我不知道如何将其响应实现到我自己的代码中以解决我的问题。

xzv2uavs

xzv2uavs1#

我已经重构了我的代码,并将旧的urlFor(filename: String) -> URL?方法替换为以下url属性:

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("test.caf")

因此,以下代码在设备上正常工作:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet weak var recordButton: UIButton!
    let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("test.caf")
    //let url = URL(fileURLWithPath: NSTemporaryDirectory().appending("test.caf")) also works
    var engine = AVAudioEngine()

    override func viewDidLoad() {
        super.viewDidLoad()

        recordButton.setTitle("Record", for: .normal)

        let file: AVAudioFile
        do {
            file = try AVAudioFile(forWriting: url, settings: engine.inputNode!.outputFormat(forBus: 0).settings)
        } catch {
            print("Error: \(error)")
            return
        }

        engine.inputNode?.installTap(onBus: 0, bufferSize: 1024, format: engine.inputNode!.outputFormat(forBus: 0)) { (buffer, time) -> Void in
            do {
                try file.write(from: buffer)
            } catch {
                print("Error: \(error)")
            }
        }
    }

    @IBAction func toggleAction(_ sender: AnyObject) {
        engine.isRunning ? stopRecording() : startRecording()
    }

    func startRecording() {
        do {
            // Start engine
            try engine.start()

            // Toggle button title
            recordButton.setTitle("Stop", for: .normal)
        } catch {
            print("Error: \(error)")
        }
    }

    func stopRecording() {
        // Stop engine
        engine.stop()

        // Toggle button title
        recordButton.setTitle("Record", for: .normal)

        print(url)
    }

}
093gszye

093gszye2#

如果有人需要在App沙盒Documents文件夹外读取AVAudioFile(例如从UIDocumentPickerViewController或SwiftUI的.fileImporter{}接收文件的URL)。然后在访问文件应用程序之前需要调用url.startAccessingSecurityScopedResource()。处理输入后,调用url.stopAccessingSecurityScopedResource()更多信息:Apple Documentation

相关问题