【问题标题】:How to record FLAC format in iOS 11+如何在 iOS 11+ 中录制 FLAC 格式
【发布时间】:2018-06-21 20:07:45
【问题描述】:

我把AudioController.swift中的代码改成了:

...
// Set format for mic input (bus 1) on RemoteIO's output scope
var asbd = AudioStreamBasicDescription()
...
asbd.mFormatID = kAudioFormatFLAC
asbd.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData
...

SpeechRecognitionService.swift

...
// send an initial request message to configure the service
let recognitionConfig = RecognitionConfig()
recognitionConfig.encoding =  .flac
...

我尝试修改 AudioStreamBasicDescription.mFormatFlags,但找不到 Google Speech API 识别格式的正确标志。

如何让 iOS 使用给定的 iOS API 录制 FLAC?

【问题讨论】:

  • 语音 API 是否返回特定的错误消息?
  • AVAudioRecorder可以直接用来录制Flac格式
  • @ChristianAbella 您ping的链接记录PCM然后转换为FLAC
  • @jspcal 代码只是暂停等待来自 AudioRecorder 的响应,这可能是我所做的。由于没有错误,我无法调试它。

标签: ios iphone swift google-speech-api flac


【解决方案1】:

试试这个

/// Common Struct 
struct Manager
{    
    //Required Objects - AVFoundation
    ///AVAudio Session
    static var recordingSession: AVAudioSession!

    ///AVAudio Recorder
    static var recorder: AVAudioRecorder?
}

权限

func CheckForPermission() {
    Manager.recordingSession = AVAudioSession.sharedInstance()
    do {
        try Manager.recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
        Manager.recordingSession.requestRecordPermission({ (allowed) in
            if allowed {
                Manager.micAuthorised = true
            }
            else {
                Manager.micAuthorised = false
            }
        })
        DispatchQueue.main.async  {
            //Reload Label text in Home screen
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateLabel"), object: nil)
        }
    }
    catch {
        print("Failed to set Category", error.localizedDescription)
    }
}

录音机设置和录音

func startMainRecording() {
    print("Main Recording session is created")

    //Get unique File Name
    Manager.audioName = getUniqueName()
    let AudioFileName = getDocumentsDirectory().appendingPathComponent("\(Manager.audioName).flac")
    print("New Path: \(AudioFileName)")

    //Recorder Settings
    let settings: [String: Any] = [
        /// Format Flac
        AVFormatIDKey: Int(kAudioFormatFLAC),
        AVSampleRateKey: 16000,
        AVNumberOfChannelsKey: 1,
        AVLinearPCMBitDepthKey: 16,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
        AVLinearPCMIsBigEndianKey: false,
        AVLinearPCMIsFloatKey: false,
        ]

    //Handler
    do {
        //Activate session when required

        //try Manager.recordingSession.setPreferredSampleRate(44000)
        try Manager.recordingSession.setActive(true)

        //Start Recording With Audio File name
        Manager.recorder = try AVAudioRecorder(url: AudioFileName, settings: settings)
        Manager.recorder?.delegate = self
        Manager.recorder?.isMeteringEnabled = true
    }
    catch
    {
        //Finish Recording with a Error
        print("Error Handling: \(error.localizedDescription)")
        /// Saved here if any error occur while recording Just case
        self.finishRecording(success: false)
    }
}

【讨论】:

  • FLAC 如果我没记错需要采样率为 44100,我会试试这个看看。
  • 我使用低采样率只是为了让我的文件更小,因为我需要上传到数据库中
  • 这有帮助吗?
  • @iOSGeek 如何停止录制,这是否可以录制带有 flac 编码音频的视频?
  • @iOSGeek 我被转移到了一个不同的项目,从来没有真正深入了解这一点。希望您的回答能帮助遇到同样问题的人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多