简体   繁体   English

AudioKit:我可以在不调用AudioKit.stop()的情况下禁用AKMicrophone吗?

[英]AudioKit: Can I disable an AKMicrophone without calling AudioKit.stop()?

I've got an app with two features. 我有一个有两个功能的应用程序。 One plays an AKMetronome and allows background playback when the app is not in the foreground. 一个人播放AKMetronome并在应用程序不在前台时允许后台播放。 The other is a tuner feature which uses AKMicrophone , which only needs to be active when the app is in the foreground. 另一种是使用AKMicrophone的调谐器功能,只有在应用程序位于前台时才需要激活。 These features can be used simultaneously (a blinking metronome light while the tuner is active). 这些功能可以同时使用(调谐器处于活动状态时闪烁的节拍器灯)。

When I press the home button, the metronome keeps playing (as expected). 当我按下主页按钮时,节拍器继续播放(如预期的那样)。 But the active microphone for the tuner also causes the bright red "recording"-style bar across the top of the device in all other apps. 但是调谐器的有源麦克风还会在所有其他应用中在设备顶部产生亮红色“录音”式条形 I don't need the tuner to function when the app isn't open though. 虽然应用程序未打开,但我不需要调谐器。

How do I disable the AKMicrophone node so that this bar doesn't show up, while also keeping the AKMetronome node playing uninterrupted? 如何禁用AKMicrophone节点以使此栏不显示,同时还保持AKMetronome节点不间断播放?

I've tried the following, all with no success: 我尝试过以下几点,但都没有成功:

  • Calling AudioKit.stop() in the app delegate applicationWillResignActive() method. 在app委托applicationWillResignActive()方法中调用AudioKit.stop() This gets rid of the recording bar, but stops the metronome. 这摆脱了录音条,但停止了节拍器。
  • Calling stop() on the AKMicrophone node during applicationWillResignActive() . applicationWillResignActive()期间在AKMicrophone节点上调用stop() applicationWillResignActive() This doesn't remove the recording bar. 这不会删除录制栏。
  • Calling stop() and disconnectOutput() on the AKMicrophone node during applicationWillResignActive() . applicationWillResignActive()期间在AKMicrophone节点上调用stop()disconnectOutput() applicationWillResignActive() This doesn't remove the recording bar either. 这也不会删除录制栏。

I suspect that I may need to kill the entire engine (ie AudioKit.stop() ), remove the microphone node from the chain, and rebuild and restart the engine, all within the applicationWillResignActive method. 我怀疑我可能需要杀死整个引擎(即AudioKit.stop() ),从链中删除麦克风节点,并重建并重新启动引擎,所有这些都在applicationWillResignActive方法中。 But that'll interrupt the tempo of the AKMetronome , so I'm hoping it doesn't come to that. 但这会打断AKMetronome的节奏,所以我希望它不会出现这种情况。

I think you should use AudioKit for the metronome, and use a part of AudioKit called the AKMicrophoneTracker for the tuner. 我认为您应该使用AudioKit作为节拍器,并使用称为AKMicrophoneTracker的AudioKit的一部分作为调谐器。

http://audiokit.io/docs/Classes/AKMicrophoneTracker.html https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/Internals/Microphone%20Tracker/AKMicrophoneTracker.swift http://audiokit.io/docs/Classes/AKMicrophoneTracker.html https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/Internals/Microphone%20Tracker/AKMicrophoneTracker.swift

AudioKit uses AVAudioEngine for its signal flow while the tracker is actually a completely separate signal flow based off of EZAudio. AudioKit使用AVAudioEngine作为其信号流,而跟踪器实际上是基于EZAudio的完全独立的信号流。 I believe the two can be used independently. 我相信这两个可以独立使用。

The way you're currently tracking the microphone probably resembles this: http://audiokit.io/playgrounds/Analysis/Tracking%20Microphone%20Input/ 您当前跟踪麦克风的方式可能类似于: http//audiokit.io/playgrounds/Analysis/Tracking%20Microphone%20Input/

You'll want to make it more like this: 你会想要更像这样:

let tracker = AKMicrophoneTracker()
tracker.start()

//: User Interface
import AudioKitUI

class LiveView: AKLiveViewController {

    var trackedAmplitudeSlider: AKSlider?
    var trackedFrequencySlider: AKSlider?

    override func viewDidLoad() {

        AKPlaygroundLoop(every: 0.1) {
            self.trackedAmplitudeSlider?.value = tracker.amplitude
            self.trackedFrequencySlider?.value = tracker.frequency
        }

        addTitle("Tracking With Microphone Tracker")

        trackedAmplitudeSlider = AKSlider(property: "Tracked Amplitude", range: 0 ... 0.8) { _ in
            // Do nothing, just for display
        }
        addView(trackedAmplitudeSlider)

        trackedFrequencySlider = AKSlider(property: "Tracked Frequency",
                                          range: 0 ... 2_400,
                                          format: "%0.3f Hz"
        ) { _ in
            // Do nothing, just for display
        }
        addView(trackedFrequencySlider)

    }
}

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM