Skip to content

Commit b84b19d

Browse files
committed
feat(AudioVisualizationView): addition play from file function
1 parent 84c3e01 commit b84b19d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

SoundWave/Classes/AudioVisualizationView.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ public class AudioVisualizationView: BaseNibView {
200200
self.currentGradientPercentage = nil
201201
}
202202

203+
// PRAGMA: - Play From File
204+
205+
public func play(from url: URL) {
206+
guard self.audioVisualizationMode == .read else {
207+
fatalError("trying to read audio visualization in write mode")
208+
}
209+
210+
let track: AVAudioFile
211+
do {
212+
track = try AVAudioFile(forReading: url)
213+
self.meteringLevels = try track.buffer().first
214+
} catch {
215+
fatalError("failed to create file from url")
216+
}
217+
218+
guard self.meteringLevels != nil else {
219+
fatalError("trying to read audio visualization of non initialized sound record")
220+
}
221+
222+
let audioNodeFileLength = AVAudioFrameCount(track.length)
223+
let duration = Double(audioNodeFileLength) / 44100.0 // Divide by the AVSampleRateKey in the recorder settings
224+
225+
self.play(for: duration)
226+
}
227+
203228
// MARK: - Mask + Gradient
204229

205230
private func drawLevelBarsMaskAndGradient(inContext context: CGContext) {

0 commit comments

Comments
 (0)