-
Notifications
You must be signed in to change notification settings - Fork 0
/
VolumeButton.swift
executable file
·84 lines (66 loc) · 2.22 KB
/
VolumeButton.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// VolumeButton.swift
// VolumeButton
//
// Created by Alberto Pasca on 25/01/17.
// Copyright © 2017 albertopasca.it. All rights reserved.
//
import UIKit
import MediaPlayer
import AVFoundation
public protocol VolumeButtonDelegate
{
func volumeChanged()
}
open class VolumeButton: UIView {
fileprivate let kMinVolume = 0.00001
fileprivate let kMaxVolume = 0.99999
open var delegate: VolumeButtonDelegate?
fileprivate let volume = MPVolumeView(frame: CGRect.zero)
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
required public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
public func destroy() {
AVAudioSession.sharedInstance().removeObserver(self, forKeyPath: "outputVolume")
}
fileprivate func setup() {
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("Unable to initialize AVAudioSession")
}
AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: [.new], context: nil)
volume.isUserInteractionEnabled = false
volume.alpha = 0.0001
volume.showsRouteButton = false
addSubview(volume)
fixMinMaxVolume()
}
fileprivate func fixMinMaxVolume() {
var val : Float = 0.0
for view in volume.subviews {
if let slider = view as? UISlider {
if slider.value == 0.0 { slider.value = Float(kMinVolume) }
if slider.value == 1.0 { slider.value = Float(kMaxVolume) }
val = slider.value
}
}
print(val)
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let change = change, let value = change[.newKey] as? Float , keyPath == "outputVolume" else { return }
if ( value == Float(kMinVolume) || value == Float(kMaxVolume) ) {
return
}
self.delegate?.volumeChanged()
fixMinMaxVolume()
}
deinit {
destroy()
}
}