@@ -37,6 +37,22 @@ public final class ImagePlayer : ObservableObject {
3737 /// Current playing frame image
3838 @Published public var currentFrame : PlatformImage ?
3939
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+
4056 /// Start the animation
4157 public func startPlaying( ) {
4258 player? . startPlaying ( )
@@ -52,38 +68,44 @@ public final class ImagePlayer : ObservableObject {
5268 player? . stopPlaying ( )
5369 }
5470
71+ /// Seek to frame and loop count
72+ public func seekToFrame( at: UInt , loopCount: UInt ) {
73+ player? . seekToFrame ( at: at, loopCount: loopCount)
74+ }
75+
5576 /// Clear the frame buffer
5677 public func clearFrameBuffer( ) {
5778 player? . clearFrameBuffer ( )
5879 }
5980
60-
6181 /// Setup the player using Animated Image
6282 /// - Parameter image: animated image
63- public func setupPlayer( image : PlatformImage ? ) {
64- if player != nil {
83+ public func setupPlayer( animatedImage : SDAnimatedImageProvider ) {
84+ if isValid {
6585 return
6686 }
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
86101 }
102+ imagePlayer. runLoopMode = runLoopMode
103+ imagePlayer. playbackRate = playbackRate
104+ imagePlayer. playbackMode = playbackMode
105+
106+ self . player = imagePlayer
107+
108+ imagePlayer. startPlaying ( )
87109 }
88110 }
89111}
0 commit comments