forked from dmrschmidt/DSWaveformImage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveformImageView.swift
56 lines (48 loc) · 1.42 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
#if os(iOS)
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 }
waveformImageDrawer.waveformImage(
fromAudioAt: audioURL,
with: configuration.with(size: bounds.size),
qos: .userInteractive
) { image in
DispatchQueue.main.async {
self.image = image
}
}
}
}
#endif