我有一个系统,它使用 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)))
}
}