我有一个系统,该系统使用AudioKit中的各种类来记录麦克风输入并将其保存到文件中,最长持续时间为30秒,同时在记录过程中,使用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)))
}
}
答案 0 :(得分:0)
我最终用Timer实现了我想要的行为,例如:
var recordingTimer = Timer!
let maxRecordingTime = 30.0
if recordingTimer == nil
{
recordingTimer = Timer.scheduledTimer(withTimeInterval: maxRecordingTime, repeats: false)
{
timer in
self.plot.pause()
}