File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -200,6 +200,31 @@ public class AudioVisualizationView: BaseNibView {
200
200
self . currentGradientPercentage = nil
201
201
}
202
202
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
+
203
228
// MARK: - Mask + Gradient
204
229
205
230
private func drawLevelBarsMaskAndGradient( inContext context: CGContext ) {
You can’t perform that action at this time.
0 commit comments