简体   繁体   English

Cocoa (macOS) 中 AVAudioRecorder 的正确用法是什么?

[英]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.

相关问题 macOS:将Swift属性暴露给Cocoa Bindings的正确方法 - macOS: Correct way of exposing Swift properties to Cocoa Bindings AVAudioRecorder的最小音频类型是什么 - What is the smallest audio type for AVAudioRecorder 可可窗口没有显示在macOS Sierra上 - Cocoa Window Not Showing on macOS Sierra 在 Cocoa macos 应用程序中捕获 SIGINT - Trap SIGINT in Cocoa macos application 在 SwiftUI 中更新 ScrollView 的正确方法是什么? (MacOS 应用程序) - What is the correct way to update ScrollView in SwiftUI? (MacOS App) AVAudioRecorder的有效“设置”键/值是什么? - What are the valid 'settings' key/values for AVAudioRecorder? 在 macOS Cocoa 基于文档的应用程序中,对于“从模板新建”功能(例如在 Logic 中),什么是好的实现策略? - In a macOS Cocoa Document-Based Application what could be a good implementation strategy for a "New From Template" feature like for example in Logic? 在 Cocoa 中,从不在应用程序包中的本地文件获取二进制数据的正确方法是什么? - In Cocoa, what is the correct way to get binary data from a local file that is not in the app bundle? 在macOS Mojave上,在cocoa应用程序中,如何使用AVSpeechSynthesizer? - On macOS Mojave, in a cocoa application, how to use AVSpeechSynthesizer? 如何获取NSMetadataItem的URL-macOS Cocoa Swift 3 - How to get URL of NSMetadataItem - macOS Cocoa Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM