gpt4 book ai didi

ios - 如何在 iOS 11+ 中录制 FLAC 格式

转载 作者:行者123 更新时间:2023-11-28 05:57:39 25 4
gpt4 key购买 nike

我更改了 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 API 让 iOS 录制 FLAC?

最佳答案

试试这个

/// 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)
}
}

录音机设置和使用 flac 录音

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)
}
}

关于ios - 如何在 iOS 11+ 中录制 FLAC 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50976733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com