Skip to main content

Recorder

This example can record the user's microphone signal into an audio file using RecorderNode. The recorded audio file is played back using AudioPlayerNode.

import SwitchboardSDK

class RecorderExample {

let audioGraph = SBAudioGraph()
let recorderNode: SBRecorderNode
let audioPlayerNode = SBAudioPlayerNode()
let audioEngine = SBAudioEngine()

var currentFormat: SBCodec = .wav

init() {
recorderNode = SBRecorderNode(
name: "TestRecorderNode",
recordingSampleRate: 48000,
numberOfRecordedChannels: 2
)

audioGraph.addNode(recorderNode)
audioGraph.addNode(audioPlayerNode)
audioGraph.connect(audioGraph.inputNode, to: recorderNode)
audioGraph.connect(audioPlayerNode, to: audioGraph.outputNode)

audioEngine.microphoneEnabled = true
audioEngine.start(audioGraph)
}

func startRecording() {
recorderNode.start()
}

func stopRecording() {
recorderNode.stop("fileName", withFormat: currentFormat)
audioPlayerNode.load(recorderNode.getFilePath(), withFormat: currentFormat)
}

func startPlayback() {
audioPlayerNode.play()
}

func stopPlayback() {
audioPlayerNode.stop()
}
}