8
8
import AVKit
9
9
10
10
class SpeedChangerPlugin : NSObject , CommonPlayerPlugin {
11
+ private var notifyView : UILabel ?
12
+ private weak var containerView : UIView ?
11
13
private weak var player : AVPlayer ?
12
14
private weak var playerVC : AVPlayerViewController ?
13
15
14
16
@Published private( set) var currentPlaySpeed : PlaySpeed = . default
15
17
18
+ func addViewToPlayerOverlay( container: UIView ) {
19
+ containerView = container
20
+ }
21
+
22
+ private func fadeOutNotifyView( ) {
23
+ UIView . animate ( withDuration: 1.0 , animations: {
24
+ self . notifyView? . alpha = 0.0 // Fade out to invisible
25
+ } ) { _ in
26
+ self . notifyView? . removeFromSuperview ( ) // Optionally remove from superview after fading out
27
+ }
28
+ }
29
+
16
30
func playerDidLoad( playerVC: AVPlayerViewController ) {
17
31
self . playerVC = playerVC
32
+
33
+ currentPlaySpeed = Settings . mediaPlayerSpeed
18
34
}
19
35
20
36
func playerDidChange( player: AVPlayer ) {
@@ -25,6 +41,32 @@ class SpeedChangerPlugin: NSObject, CommonPlayerPlugin {
25
41
playerVC? . selectSpeed ( AVPlaybackSpeed ( rate: currentPlaySpeed. value, localizedName: currentPlaySpeed. name) )
26
42
}
27
43
44
+ func playerDidStart( player: AVPlayer ) {
45
+ if notifyView == nil {
46
+ notifyView = UILabel ( )
47
+ notifyView? . backgroundColor = UIColor . black. withAlphaComponent ( 0.3 )
48
+ notifyView? . textColor = UIColor . white
49
+ containerView? . addSubview ( notifyView!)
50
+ notifyView? . numberOfLines = 0
51
+ notifyView? . layer. cornerRadius = 10 // Set the corner radius
52
+ notifyView? . layer. masksToBounds = true // Enable masks to bounds
53
+ notifyView? . font = UIFont . systemFont ( ofSize: 26 )
54
+ notifyView? . textAlignment = NSTextAlignment . center
55
+ notifyView? . snp. makeConstraints { make in
56
+ // make.bottom.equalToSuperview().inset(20) // 20 points from the bottom
57
+ make. center. equalToSuperview ( ) // Center horizontally
58
+ make. width. equalTo ( 300 ) // Set a width (optional)
59
+ make. height. equalTo ( 60 ) // Set a height (optional)
60
+ }
61
+ }
62
+ notifyView? . isHidden = false
63
+ notifyView? . text = " 播放速度设置为 \( currentPlaySpeed. name) "
64
+
65
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 2 ) {
66
+ self . fadeOutNotifyView ( )
67
+ }
68
+ }
69
+
28
70
func addMenuItems( current: inout [ UIMenuElement ] ) -> [ UIMenuElement ] {
29
71
let gearImage = UIImage ( systemName: " gearshape " )
30
72
@@ -43,7 +85,7 @@ class SpeedChangerPlugin: NSObject, CommonPlayerPlugin {
43
85
}
44
86
}
45
87
46
- struct PlaySpeed {
88
+ struct PlaySpeed : Codable {
47
89
var name : String
48
90
var value : Float
49
91
}
0 commit comments