【问题标题】:Generate AVAudioPCMBuffer with AVAudioRecorder使用 AVAudioRecorder 生成 AVAudioPCMBuffer
【发布时间】:2016-10-23 14:27:12
【问题描述】:

随着 iOS 10,苹果发布了一个新的语音识别框架。可以通过附加 AVAudioPCMBuffers 或将 URL 提供给 m4a 将数据传递到此框架。目前,语音识别使用前者,但这只有在有人完成后才有可能,而且不是实时的。这是代码:

let audioSession = AVAudioSession.sharedInstance()
var audioRecorder:AVAudioRecorder!;
var soundURLGlobal:URL!;

function setUp(){
    let recordSettings = [AVSampleRateKey : NSNumber(value: Float(44100.0)),
                          AVFormatIDKey : NSNumber(value: Int32(kAudioFormatMPEG4AAC)),
                          AVNumberOfChannelsKey : NSNumber(value: 1),
                          AVEncoderAudioQualityKey : NSNumber(value: Int32(AVAudioQuality.medium.rawValue))]

    let fileManager = FileManager.default()
    let urls = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)
    let documentDirectory = urls[0] as NSURL
    let soundURL = documentDirectory.appendingPathComponent("sound.m4a")
    soundURLGlobal=soundURL;


    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try audioRecorder = AVAudioRecorder(url: soundURL!, settings: recordSettings)
        audioRecorder.prepareToRecord()
    } catch {}
}

function start(){
    do {
        try audioSession.setActive(true)
        audioRecorder.record()
    } catch {}
}

function stop(){
    audioRecorder.stop()
    let request=SFSpeechURLRecognitionRequest(url: soundURLGlobal!)
    let recognizer=SFSpeechRecognizer();
    recognizer?.recognitionTask(with: request, resultHandler: { (result, error) in
        if(result!.isFinal){
            print(result?.bestTranscription.formattedString)
        }
    })

}

我正在尝试转换它,但我找不到获取 AVAudioPCMBuffer 的位置。

谢谢,

【问题讨论】:

    标签: swift ios10


    【解决方案1】:

    好话题。

    你好 B 人

    这里有解决方案的主题 Tap Mic Input Using AVAudioEngine in Swift

    参见 2014 年 Wwdc 讲座 502 - 实践中的 AVAudioEngine 捕获麦克风 => 在 20 分钟内 在 21 .50 中使用点击代码 => 创建缓冲区

    这里是 swift 3 代码

    @IBAction func button01Pressed(_ sender: Any) {
    
        let inputNode = audioEngine.inputNode
        let bus = 0
        inputNode?.installTap(onBus: bus, bufferSize: 2048, format: inputNode?.inputFormat(forBus: bus)) {
            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
    
                var theLength = Int(buffer.frameLength)
                print("theLength = \(theLength)")
    
                var samplesAsDoubles:[Double] = []
                for i in 0 ..< Int(buffer.frameLength)
                {
                    var theSample = Double((buffer.floatChannelData?.pointee[i])!)
                    samplesAsDoubles.append( theSample )
                }
    
                print("samplesAsDoubles.count = \(samplesAsDoubles.count)")
    
        }
    
        audioEngine.prepare()
        try! audioEngine.start()
    
    }
    

    停止音频

    func stopAudio()
        {
    
            let inputNode = audioEngine.inputNode
            let bus = 0
            inputNode?.removeTap(onBus: bus)
            self.audioEngine.stop()
    
        }
    

    【讨论】:

    • 您好,我想作者是在问如何使用 AVAudioRecorder,而不是 AVAudioEngine。
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2020-12-16
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多