@@ -37,6 +37,22 @@ public final class ImagePlayer : ObservableObject {
37
37
/// Current playing frame image
38
38
@Published public var currentFrame : PlatformImage ?
39
39
40
+ /// Current playing frame index
41
+ @Published public var currentFrameIndex : UInt = 0
42
+
43
+ /// Current playing loop count
44
+ @Published public var currentLoopCount : UInt = 0
45
+
46
+ /// Whether current player is valid for playing. This will check the internal player exist or not
47
+ public var isValid : Bool {
48
+ player != nil
49
+ }
50
+
51
+ /// Current playing status
52
+ public var isPlaying : Bool {
53
+ player? . isPlaying ?? false
54
+ }
55
+
40
56
/// Start the animation
41
57
public func startPlaying( ) {
42
58
player? . startPlaying ( )
@@ -52,38 +68,44 @@ public final class ImagePlayer : ObservableObject {
52
68
player? . stopPlaying ( )
53
69
}
54
70
71
+ /// Seek to frame and loop count
72
+ public func seekToFrame( at: UInt , loopCount: UInt ) {
73
+ player? . seekToFrame ( at: at, loopCount: loopCount)
74
+ }
75
+
55
76
/// Clear the frame buffer
56
77
public func clearFrameBuffer( ) {
57
78
player? . clearFrameBuffer ( )
58
79
}
59
80
60
-
61
81
/// Setup the player using Animated Image
62
82
/// - Parameter image: animated image
63
- public func setupPlayer( image : PlatformImage ? ) {
64
- if player != nil {
83
+ public func setupPlayer( animatedImage : SDAnimatedImageProvider ) {
84
+ if isValid {
65
85
return
66
86
}
67
- if let animatedImage = image as? SDAnimatedImageProvider & PlatformImage {
68
- if let imagePlayer = SDAnimatedImagePlayer ( provider: animatedImage) {
69
- imagePlayer. animationFrameHandler = { [ weak self] ( _, frame) in
70
- self ? . currentFrame = frame
71
- }
72
- // Setup configuration
73
- if let maxBufferSize = maxBufferSize {
74
- imagePlayer. maxBufferSize = maxBufferSize
75
- }
76
- if let customLoopCount = customLoopCount {
77
- imagePlayer. totalLoopCount = customLoopCount
78
- }
79
- imagePlayer. runLoopMode = runLoopMode
80
- imagePlayer. playbackRate = playbackRate
81
- imagePlayer. playbackMode = playbackMode
82
-
83
- self . player = imagePlayer
84
-
85
- imagePlayer. startPlaying ( )
87
+ if let imagePlayer = SDAnimatedImagePlayer ( provider: animatedImage) {
88
+ imagePlayer. animationFrameHandler = { [ weak self] ( index, frame) in
89
+ self ? . currentFrameIndex = index
90
+ self ? . currentFrame = frame
91
+ }
92
+ imagePlayer. animationLoopHandler = { [ weak self] ( loopCount) in
93
+ self ? . currentLoopCount = loopCount
94
+ }
95
+ // Setup configuration
96
+ if let maxBufferSize = maxBufferSize {
97
+ imagePlayer. maxBufferSize = maxBufferSize
98
+ }
99
+ if let customLoopCount = customLoopCount {
100
+ imagePlayer. totalLoopCount = customLoopCount
86
101
}
102
+ imagePlayer. runLoopMode = runLoopMode
103
+ imagePlayer. playbackRate = playbackRate
104
+ imagePlayer. playbackMode = playbackMode
105
+
106
+ self . player = imagePlayer
107
+
108
+ imagePlayer. startPlaying ( )
87
109
}
88
110
}
89
111
}
0 commit comments