Skip to content

Commit cd6cca8

Browse files
committed
fix: audio could not play,fps fault-tolerant
1 parent 74f7ecc commit cd6cca8

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

SVGAPlayer.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */ = {isa = PBXBuildFile; fileRef = 90D7CA1A1F7FB114006E74F0 /* rose_1.5.0.svga */; };
5959
90D7CA1E1F7FB34E006E74F0 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 90D7CA1D1F7FB34E006E74F0 /* libz.tbd */; };
6060
90DB59B51F96026E00894727 /* SVGAImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 90DB59B41F96026E00894727 /* SVGAImageView.m */; };
61+
E83A58D9247E0E6A00D9F404 /* audio_biling.svga in Resources */ = {isa = PBXBuildFile; fileRef = E83A58D8247E0E6A00D9F404 /* audio_biling.svga */; };
6162
/* End PBXBuildFile section */
6263

6364
/* Begin PBXFileReference section */
@@ -119,6 +120,7 @@
119120
90DB59B41F96026E00894727 /* SVGAImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAImageView.m; sourceTree = "<group>"; };
120121
92332F7A897BF4379D765B05 /* libPods-SVGAPlayer React.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SVGAPlayer React.a"; sourceTree = BUILT_PRODUCTS_DIR; };
121122
E02B8713B25C0283C736EE03 /* Pods-SVGAPlayer.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SVGAPlayer.release.xcconfig"; path = "Pods/Target Support Files/Pods-SVGAPlayer/Pods-SVGAPlayer.release.xcconfig"; sourceTree = "<group>"; };
123+
E83A58D8247E0E6A00D9F404 /* audio_biling.svga */ = {isa = PBXFileReference; lastKnownFileType = file; path = audio_biling.svga; sourceTree = "<group>"; };
122124
FF89C40C3E9839DA5DE71191 /* Pods-SVGAPlayer React.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SVGAPlayer React.release.xcconfig"; path = "Pods/Target Support Files/Pods-SVGAPlayer React/Pods-SVGAPlayer React.release.xcconfig"; sourceTree = "<group>"; };
123125
/* End PBXFileReference section */
124126

@@ -249,6 +251,7 @@
249251
90D7C9FA1F7E2AA3006E74F0 /* Samples */ = {
250252
isa = PBXGroup;
251253
children = (
254+
E83A58D8247E0E6A00D9F404 /* audio_biling.svga */,
252255
718A146C235718E000FED5D3 /* Rocket.svga */,
253256
71DAA8A42355B3ED006608A1 /* Goddess.svga */,
254257
71A8679022B7785100176CD6 /* matteRect.svga */,
@@ -373,6 +376,7 @@
373376
71A8679322B7853600176CD6 /* matteBitmap.svga in Resources */,
374377
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */,
375378
90D7CA1B1F7FB114006E74F0 /* rose_2.0.0.svga in Resources */,
379+
E83A58D9247E0E6A00D9F404 /* audio_biling.svga in Resources */,
376380
71DAA8A52355B3ED006608A1 /* Goddess.svga in Resources */,
377381
);
378382
runOnlyForDeploymentPostprocessing = 0;
16.1 KB
Binary file not shown.

Source/SVGAAudioLayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
@property (nonatomic, readonly) AVAudioPlayer *audioPlayer;
1717
@property (nonatomic, readonly) SVGAAudioEntity *audioItem;
18+
@property (nonatomic, assign) BOOL audioPlaying;
19+
1820

1921
- (instancetype)initWithAudioItem:(SVGAAudioEntity *)audioItem videoItem:(SVGAVideoEntity *)videoItem;
2022

Source/SVGAPlayer.m

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ @interface SVGAPlayer ()
3131
@property (nonatomic, assign) NSRange currentRange;
3232
@property (nonatomic, assign) BOOL forwardAnimating;
3333
@property (nonatomic, assign) BOOL reversing;
34-
@property (nonatomic, assign) BOOL audioPlaying;
3534

3635
@end
3736

@@ -79,8 +78,11 @@ - (void)startAnimation {
7978
}
8079
[self stopAnimation:NO];
8180
self.loopCount = 0;
81+
if (self.videoItem.FPS == 0) {
82+
NSLog(@"videoItem FPS could not be 0!");
83+
return;
84+
}
8285
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
83-
8486
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
8587
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
8688
self.forwardAnimating = !self.reversing;
@@ -125,13 +127,12 @@ - (void)clear {
125127
}
126128

127129
- (void)clearAudios {
128-
if (!self.audioPlaying) {
129-
return;
130-
}
131130
for (SVGAAudioLayer *layer in self.audioLayers) {
132-
[layer.audioPlayer stop];
131+
if (layer.audioPlaying) {
132+
[layer.audioPlayer stop];
133+
layer.audioPlaying = NO;
134+
}
133135
}
134-
self.audioPlaying = NO;
135136
}
136137

137138
- (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
@@ -149,6 +150,10 @@ - (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
149150
[self update];
150151
if (andPlay) {
151152
self.forwardAnimating = YES;
153+
if (self.videoItem.FPS == 0) {
154+
NSLog(@"videoItem FPS could not be 0!");
155+
return;
156+
}
152157
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
153158
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
154159
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
@@ -324,14 +329,14 @@ - (void)update {
324329
[CATransaction setDisableActions:NO];
325330
if (self.forwardAnimating && self.audioLayers.count > 0) {
326331
for (SVGAAudioLayer *layer in self.audioLayers) {
327-
if (!self.audioPlaying && layer.audioItem.startFrame >= self.currentFrame) {
332+
if (!layer.audioPlaying && layer.audioItem.startFrame <= self.currentFrame && self.currentFrame <= layer.audioItem.endFrame) {
328333
[layer.audioPlayer setCurrentTime:(NSTimeInterval)(layer.audioItem.startTime / 1000)];
329334
[layer.audioPlayer play];
330-
self.audioPlaying = YES;
335+
layer.audioPlaying = YES;
331336
}
332-
if (self.audioPlaying && layer.audioItem.endFrame <= self.currentFrame) {
337+
if (layer.audioPlaying && layer.audioItem.endFrame <= self.currentFrame) {
333338
[layer.audioPlayer stop];
334-
self.audioPlaying = NO;
339+
layer.audioPlaying = NO;
335340
}
336341
}
337342
}

0 commit comments

Comments
 (0)