@@ -407,6 +407,14 @@ class StateMachine {
407
407
this . instance . advance ( time ) ;
408
408
}
409
409
410
+ /**
411
+ * Advances the state machine instance by a given time and apply changes to artboard.
412
+ * @param time - the time to advance the animation by in seconds
413
+ */
414
+ public advanceAndApply ( time : number ) {
415
+ this . instance . advanceAndApply ( time ) ;
416
+ }
417
+
410
418
/**
411
419
* Returns the number of events reported from the last advance call
412
420
* @returns Number of events reported
@@ -1564,6 +1572,9 @@ export class Rive {
1564
1572
// Whether the canvas element's size is 0
1565
1573
private _hasZeroSize = false ;
1566
1574
1575
+ // Audio event listener
1576
+ private _audioEventListener : EventListener | null = null ;
1577
+
1567
1578
// Durations to generate a frame for the last second. Used for performance profiling.
1568
1579
public durations : number [ ] = [ ] ;
1569
1580
public frameTimes : number [ ] = [ ] ;
@@ -1774,11 +1785,12 @@ export class Rive {
1774
1785
private initializeAudio ( ) {
1775
1786
// Initialize audio if needed
1776
1787
if ( audioManager . status == SystemAudioStatus . UNAVAILABLE ) {
1777
- if ( this . artboard ?. hasAudio ) {
1778
- audioManager . add ( {
1788
+ if ( this . artboard ?. hasAudio && this . _audioEventListener === null ) {
1789
+ this . _audioEventListener = {
1779
1790
type : EventType . AudioStatusChange ,
1780
1791
callback : ( ) => this . onSystemAudioChanged ( ) ,
1781
- } ) ;
1792
+ } ;
1793
+ audioManager . add ( this . _audioEventListener ) ;
1782
1794
audioManager . establishAudio ( ) ;
1783
1795
}
1784
1796
}
@@ -2009,13 +2021,15 @@ export class Rive {
2009
2021
}
2010
2022
}
2011
2023
}
2012
- stateMachine . advance ( elapsedTime ) ;
2024
+ stateMachine . advanceAndApply ( elapsedTime ) ;
2013
2025
// stateMachine.instance.apply(this.artboard);
2014
2026
}
2015
2027
2016
2028
// Once the animations have been applied to the artboard, advance it
2017
2029
// by the elapsed time.
2018
- this . artboard . advance ( elapsedTime ) ;
2030
+ if ( this . animator . stateMachines . length == 0 ) {
2031
+ this . artboard . advance ( elapsedTime ) ;
2032
+ }
2019
2033
2020
2034
const { renderer } = this ;
2021
2035
// Canvas must be wiped to prevent artifacts
@@ -2125,6 +2139,10 @@ export class Rive {
2125
2139
this . riveFile = null ;
2126
2140
this . file = null ;
2127
2141
this . deleteRiveRenderer ( ) ;
2142
+ if ( this . _audioEventListener !== null ) {
2143
+ audioManager . remove ( this . _audioEventListener ) ;
2144
+ this . _audioEventListener = null ;
2145
+ }
2128
2146
}
2129
2147
2130
2148
/**
0 commit comments