-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
WaveformImageView.swift
64 lines (54 loc) · 1.68 KB
/
WaveformImageView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#if os(iOS) || swift(>=5.9) && os(visionOS)
import DSWaveformImage
import Foundation
import AVFoundation
import UIKit
public class WaveformImageView: UIImageView {
private let waveformImageDrawer: WaveformImageDrawer
public var configuration: Waveform.Configuration {
didSet { updateWaveform() }
}
public var waveformAudioURL: URL? {
didSet { updateWaveform() }
}
override public init(frame: CGRect) {
configuration = Waveform.Configuration(size: frame.size)
waveformImageDrawer = WaveformImageDrawer()
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
configuration = Waveform.Configuration()
waveformImageDrawer = WaveformImageDrawer()
super.init(coder: aDecoder)
}
override public func layoutSubviews() {
super.layoutSubviews()
updateWaveform()
}
/// Clears the audio data, emptying the waveform view.
public func reset() {
waveformAudioURL = nil
image = nil
}
}
private extension WaveformImageView {
func updateWaveform() {
guard let audioURL = waveformAudioURL else { return }
Task {
do {
let image = try await waveformImageDrawer.waveformImage(
fromAudioAt: audioURL,
with: configuration.with(size: bounds.size),
qos: .userInteractive
)
await MainActor.run {
self.image = image
}
} catch {
print("Error occurred during waveform image creation:")
print(error)
}
}
}
}
#endif