[英]How to implement a listener or while(true) to stop a waveform plot from scrolling once recorder has finished recording in Swift using AudioKit
我有一個系統,該系統使用AudioKit中的各種類來記錄麥克風輸入並將其保存到文件中,最長持續時間為30s,同時在記錄過程中,使用EZAudioPlot將輸出波形繪制到波形圖上。
我的問題是我使用的是Snapchat風格的錄制按鈕,該按鈕在觸地事件時開始錄制/繪圖,而在觸地事件(內部和外部)事件時停止錄制/繪圖-這意味着當用戶按住錄制按鈕時超過最大持續時間(有時會這樣做),盡管錄音機完成了,但麥克風輸出的波形仍在繪制。 我問Swift / AudioKit中是否有一種方法可以不斷“監聽”錄音機停止錄制,類似於
while true
{
if recorder.isRecording() == false
{
plot.pause()
}
}
在按下按鈕之后但在釋放按鈕之前? 如果有某種方法可以無限期地收聽錄音機在按下按鈕和釋放按鈕之間完成錄制的過程 ,那么這很容易解決我的問題。 這樣的功能在Swift或AudioKit中是否以某種形式存在?
我的代碼如下(省略了不相關的代碼):
import UIKit
import AudioKit
import AudioKitUI
// Define maximum recording time in seconds
let maxRecordingTime = 30.0
class ViewController: UIViewController
{
var microphone : AKMicrophone!
var mixer : AKMixer!
var waveformBooster: AKBooster!
var outputBooster : AKBooster!
var exportTape : AKAudioFile!
var recorder : AKNodeRecorder!
var player : AKClipPlayer!
var circleView : CircleView!
var plot : AKNodeOutputPlot!
@IBOutlet var startRecordingButton: CircularButton!
@IBOutlet var playRecordingButton: UIButton!
@IBOutlet var waveformPlot: EZAudioPlot!
override func viewDidLoad()
{
super.viewDidLoad()
microphone = AKMicrophone()
mixer = AKMixer(microphone)
// Initialise booster to set monitoring level to zero - this ensures that
// microphone output is recorded but not sent to main audio output
outputBooster = AKBooster(mixer)
outputBooster.gain = 0
// Initialise booster to set waveform display gain so that waveform can be set to display
// only when the app is recording
waveformBooster = AKBooster(microphone)
waveformBooster.gain = 0
AudioKit.output = outputBooster
try!AudioKit.start()
// Initialise file to store recorder output and set recorder to route mixer
// output to file
exportTape = try! AKAudioFile(name: "ExportTape")
recorder = try! AKNodeRecorder(node: mixer, file: exportTape)
recorder.durationToRecord = maxRecordingTime
plot = AKNodeOutputPlot(waveformBooster, frame: waveformPlot.bounds)
setupWaveformPlot()
}
@IBAction func startRecording(_ sender: UIButton)
{
// Delete contents of output file so it can be rewritten
try! recorder.reset()
// Perform various tasks related to getting the plot ready
// to be rewritten to in the event of several recordings being made
updateWaveformPlot()
// Start the microphone and
microphone.start()
waveformBooster.gain = 1
animateRecordingButton()
do
{
try recorder?.record()
} catch
{
AKLog("Couldn't record")
}
}
@IBAction func stopRecording(_ sender: UIButton)
{
microphone.stop()
waveformBooster.gain = 0
recorder.stop()
plot.pause()
}
@IBAction func playRecording(_ sender: UIButton)
{
let player = try! AKAudioPlayer(file: exportTape)
if player.isStarted == false
{
AudioKit.output = player
player.play()
}
}
func setupWaveformPlot()
{
plot.plotType = .rolling
plot.clipsToBounds = true
plot.shouldFill = true
plot.shouldMirror = true
plot.color = UIColor.white
plot.backgroundColor = UIColor.black
// Set the gain of the plot to control range of values displayed on the waveform plot
plot.gain = 25
waveformPlot.addSubview(plot)
}
func updateWaveformPlot()
{
plot.clear()
plot.resume()
// Set rolling history length to correct value for 30s
// such that waveform fills whole plot with no scrolling
plot.setRollingHistoryLength(Int32(Float(1284)))
}
}
我最終用Timer實現了我想要的行為,例如:
var recordingTimer = Timer!
let maxRecordingTime = 30.0
if recordingTimer == nil
{
recordingTimer = Timer.scheduledTimer(withTimeInterval: maxRecordingTime, repeats: false)
{
timer in
self.plot.pause()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.