[英]What is correct usage of AVAudioRecorder in Cocoa (macOS)?
I have a simple app where I try to do audio recordings on macOS using Swift and Cocoa.我有一个简单的应用程序,我尝试使用 Swift 和 Cocoa 在 macOS 上进行录音。 I get an audio file produced but with nothing in it.
我得到了一个音频文件,但里面没有任何内容。 The whole app is basically in an
NSWindowController
.整个应用程序基本上都在一个
NSWindowController
。 It has this relevant code.它有这个相关的代码。
// member variable for recorder
var recorder : AVAudioRecorder?
// function called indirectly from UI to begin recording
func startRecording() throws {
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
let rec = try AVAudioRecorder(url: filename, settings: settings)
rec.delegate = self
rec.prepareToRecord()
rec.isMeteringEnabled = true
rec.record()
self.recorder = rec
}
// Callback I use to stop recording
@IBAction func stop(sender: AnyObject) {
self.recorder?.stop()
self.recorder = nil
}
For Signing & Capabilities I have the following checked.对于签名和功能,我检查了以下内容。 I am not sure if my code is wrong or if there is something wrong with my capabilities.
我不确定我的代码是否有问题,或者我的能力是否有问题。
The strange thing is that I get this style of code working in Playgrounds.奇怪的是,我在 Playgrounds 中得到了这种风格的代码。 This code will work in playgrounds, but it doesn't look fundamentally different from my GUI app code:
此代码将在 Playgrounds 中工作,但它看起来与我的 GUI 应用程序代码没有根本的不同:
import Speech
let paths = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask)
let docsDir = paths[0]
let filename = docsDir.appendingPathComponent("voiceRec.m4a")
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
let rec = try AVAudioRecorder(
url: filename,
settings: settings)
var ok = rec.prepareToRecord()
ok = rec.record()
// Evaluate this in playground when you are done recording
rec.stop()
You need to add the NSMicrophoneUsageDescription
key to your Info.plist
file.您需要将
NSMicrophoneUsageDescription
键添加到您的Info.plist
文件中。 This is required to allow access to the microphone.这是允许访问麦克风所必需的。 Remember both iOS and macOS run in sandbox and will not allow the app to do anything it has not been given explicit permission to do.
请记住,iOS 和 macOS 都在沙箱中运行,并且不会允许应用程序执行任何未获得明确许可的操作。 The
NSMicrophoneUsageDescription
has to have a description of what the microphone will be used for so that a user of the application can read the reason given and judge if it is valid, or whether that particular feature is something they want to use. NSMicrophoneUsageDescription
必须描述麦克风的用途,以便应用程序的用户可以阅读给出的原因并判断它是否有效,或者该特定功能是否是他们想要使用的东西。
You just paste the key in as shown.您只需粘贴密钥,如图所示。 You need to add another entry by clicking the plus (+) button on a row above.
您需要通过单击上方一行的加号 (+) 按钮来添加另一个条目。
Also make sure that microphone input is enabled under capabilities:还要确保在以下功能下启用麦克风输入:
You can see an example of how the Info.plist should look您可以看到 Info.plist 的外观示例
<key>NSMicrophoneUsageDescription</key>
<string>Record audio to file to later transcribe</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>For transcribing recorded audio</string>
Thanks to @jnpdx for clarifying this point in a comment.感谢 @jnpdx 在评论中澄清这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.