我一直在尝试将Microhpone与AudioKit一起使用。
代码编译并运行它还请求访问麦克风的权限,但所有频率和幅度读数均为0。
这是我编写的用于将麦克风读数作为事件发送的类。
[我看过其他一些本来可以理解的问题,并像我尝试更改输入但没有运气一样尝试了所有问题。
我的猜测是我无法正确理解AudioKit的生命周期,或者@objc标签更改了行为。
此代码曾经在旧版ios上运行,但在13.3上似乎有所更改。 (@objc的原因是我需要在react native bridge中使用它)
我以为这可能与info.plist有关,但我已经为字符串隐私配置了info.plist,但仍然没有运气。
我想念什么吗?
谢谢请在此处检查代码:
import Foundation
import AudioKit
@objc(AudioKitWrapper)
class AudioKitWrapper: NSObject {
@objc var mic: AKMicrophone!
@objc var timer: Timer!
@objc var tracker: AKFrequencyTracker!
override init(){
super.init()
do {
try AKSettings.setSession(category: .playAndRecord)
} catch {
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true
if let inputs = AudioKit.inputDevices {
do {
print(inputs)
try AudioKit.setInputDevice(inputs[0])
AKSettings.audioInputEnabled = true
mic = AKMicrophone()
try mic?.setDevice(inputs[0])
}
catch {
print("microphone not supported")
}
}
try? AudioKit.start()
mic?.stop()
self.tracker = AKFrequencyTracker.init(mic, hopSize: 4_096, peakCount: 20)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.loop), userInfo: nil, repeats: true)
}
@objc func start() {
if (mic?.isStarted == true) {
print("stop")
mic?.stop()
try! AudioKit.stop()
}
else {
print("start")
mic?.start()
// try! AudioKit.start()
var silence = AKBooster(tracker, gain: 0)
AudioKit.output = silence
}
}
@objc static func requiresMainQueueSetup() -> Bool {
return false
}
@objc func loop() {
if (mic?.isStarted == true){
print(self.tracker.amplitude, self.tracker.frequency)
EventEmitter.sharedInstance.dispatch(name: "onChange",
body: ["amplitude": tracker.amplitude, "frequency":tracker.frequency])
}
}
}
我观看了从沙盒到制作的视频,并做了一些改动。
我意识到我代码的主要问题不是使用AKBooster。将变量声明更改为提到的视频,然后开始工作。
以下是有效的快速代码:
import Foundation
import AudioKit
@objc(AudioKitWrapper)
class AudioKitWrapper: NSObject {
@objc var mic: AKMicrophone!
@objc var timer: Timer!
@objc var tracker: AKFrequencyTracker!
@objc var silence: AKBooster!
@objc var micCopy1: AKBooster!
@objc override init(){
super.init()
mic = AKMicrophone()
micCopy1 = AKBooster(mic)
do {
try AKSettings.setSession(category: .playAndRecord)
AKSettings.defaultToSpeaker = true
} catch {
print("Could not set session category.")
return
}
if let inputs = AudioKit.inputDevices {
do {
try AudioKit.setInputDevice(inputs[0])
try mic.setDevice(inputs[0])
}
catch {
print("microphone not supported")
return
}
}
do {
tracker = AKFrequencyTracker.init(micCopy1, hopSize: 4_096, peakCount: 20)
silence = AKBooster(tracker, gain: 0)
AudioKit.output = silence
try AudioKit.start()
mic.stop()
}
catch {
print("AudioKit did not start")
}
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(loop), userInfo: nil, repeats: true)
}
@objc func start() {
print("started?", mic?.isStarted == true)
if (mic?.isStarted == true) {
print("stop")
mic?.stop()
}
else {
print("start")
mic.start()
}
}
@objc static func requiresMainQueueSetup() -> Bool {
return true
}
@objc func loop() {
if (mic.isStarted == true){
print("dispatch", tracker.frequency, tracker.amplitude)
EventEmitter.sharedInstance.dispatch(name: "onChange",
body: ["amplitude": self.tracker.amplitude, "frequency": self.tracker.frequency])
}
}
}
同样也有兴趣让AudioKit在React Native上工作的人,这里是ObjectiveC代码:
AudioKitBridge.m
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(AudioKitWrapper, NSObject)
RCT_EXTERN_METHOD(start)
@end
AudioKitBridge.h
#ifndef AudioKitBridge_h
#define AudioKitBridge_h
#import <React/RCTBridgeModule.h>
@interface AudioKitBridge: NSObject
@end