Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
fix: audio could not play,fps fault-tolerant
Browse files Browse the repository at this point in the history
  • Loading branch information
yangqingren committed May 27, 2020
1 parent 74f7ecc commit cd6cca8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions SVGAPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */ = {isa = PBXBuildFile; fileRef = 90D7CA1A1F7FB114006E74F0 /* rose_1.5.0.svga */; };
90D7CA1E1F7FB34E006E74F0 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 90D7CA1D1F7FB34E006E74F0 /* libz.tbd */; };
90DB59B51F96026E00894727 /* SVGAImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 90DB59B41F96026E00894727 /* SVGAImageView.m */; };
E83A58D9247E0E6A00D9F404 /* audio_biling.svga in Resources */ = {isa = PBXBuildFile; fileRef = E83A58D8247E0E6A00D9F404 /* audio_biling.svga */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -119,6 +120,7 @@
90DB59B41F96026E00894727 /* SVGAImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAImageView.m; sourceTree = "<group>"; };
92332F7A897BF4379D765B05 /* libPods-SVGAPlayer React.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SVGAPlayer React.a"; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
E83A58D8247E0E6A00D9F404 /* audio_biling.svga */ = {isa = PBXFileReference; lastKnownFileType = file; path = audio_biling.svga; sourceTree = "<group>"; };
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>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -249,6 +251,7 @@
90D7C9FA1F7E2AA3006E74F0 /* Samples */ = {
isa = PBXGroup;
children = (
E83A58D8247E0E6A00D9F404 /* audio_biling.svga */,
718A146C235718E000FED5D3 /* Rocket.svga */,
71DAA8A42355B3ED006608A1 /* Goddess.svga */,
71A8679022B7785100176CD6 /* matteRect.svga */,
Expand Down Expand Up @@ -373,6 +376,7 @@
71A8679322B7853600176CD6 /* matteBitmap.svga in Resources */,
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */,
90D7CA1B1F7FB114006E74F0 /* rose_2.0.0.svga in Resources */,
E83A58D9247E0E6A00D9F404 /* audio_biling.svga in Resources */,
71DAA8A52355B3ED006608A1 /* Goddess.svga in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file added SVGAPlayer/Samples/audio_biling.svga
Binary file not shown.
2 changes: 2 additions & 0 deletions Source/SVGAAudioLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

@property (nonatomic, readonly) AVAudioPlayer *audioPlayer;
@property (nonatomic, readonly) SVGAAudioEntity *audioItem;
@property (nonatomic, assign) BOOL audioPlaying;


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

Expand Down
27 changes: 16 additions & 11 deletions Source/SVGAPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ @interface SVGAPlayer ()
@property (nonatomic, assign) NSRange currentRange;
@property (nonatomic, assign) BOOL forwardAnimating;
@property (nonatomic, assign) BOOL reversing;
@property (nonatomic, assign) BOOL audioPlaying;

@end

Expand Down Expand Up @@ -79,8 +78,11 @@ - (void)startAnimation {
}
[self stopAnimation:NO];
self.loopCount = 0;
if (self.videoItem.FPS == 0) {
NSLog(@"videoItem FPS could not be 0!");
return;
}
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];

self.displayLink.frameInterval = 60 / self.videoItem.FPS;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
self.forwardAnimating = !self.reversing;
Expand Down Expand Up @@ -125,13 +127,12 @@ - (void)clear {
}

- (void)clearAudios {
if (!self.audioPlaying) {
return;
}
for (SVGAAudioLayer *layer in self.audioLayers) {
[layer.audioPlayer stop];
if (layer.audioPlaying) {
[layer.audioPlayer stop];
layer.audioPlaying = NO;
}
}
self.audioPlaying = NO;
}

- (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
Expand All @@ -149,6 +150,10 @@ - (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
[self update];
if (andPlay) {
self.forwardAnimating = YES;
if (self.videoItem.FPS == 0) {
NSLog(@"videoItem FPS could not be 0!");
return;
}
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
Expand Down Expand Up @@ -324,14 +329,14 @@ - (void)update {
[CATransaction setDisableActions:NO];
if (self.forwardAnimating && self.audioLayers.count > 0) {
for (SVGAAudioLayer *layer in self.audioLayers) {
if (!self.audioPlaying && layer.audioItem.startFrame >= self.currentFrame) {
if (!layer.audioPlaying && layer.audioItem.startFrame <= self.currentFrame && self.currentFrame <= layer.audioItem.endFrame) {
[layer.audioPlayer setCurrentTime:(NSTimeInterval)(layer.audioItem.startTime / 1000)];
[layer.audioPlayer play];
self.audioPlaying = YES;
layer.audioPlaying = YES;
}
if (self.audioPlaying && layer.audioItem.endFrame <= self.currentFrame) {
if (layer.audioPlaying && layer.audioItem.endFrame <= self.currentFrame) {
[layer.audioPlayer stop];
self.audioPlaying = NO;
layer.audioPlaying = NO;
}
}
}
Expand Down

0 comments on commit cd6cca8

Please sign in to comment.