@@ -29,7 +29,9 @@ class SoundLibrary extends React.PureComponent {
29
29
bindAll ( this , [
30
30
'handleItemSelected' ,
31
31
'handleItemMouseEnter' ,
32
- 'handleItemMouseLeave'
32
+ 'handleItemMouseLeave' ,
33
+ 'onStop' ,
34
+ 'setStopHandler'
33
35
] ) ;
34
36
35
37
/**
@@ -43,6 +45,11 @@ class SoundLibrary extends React.PureComponent {
43
45
* @type {Promise<SoundPlayer> }
44
46
*/
45
47
this . playingSoundPromise = null ;
48
+
49
+ /**
50
+ * function to call when the sound ends
51
+ */
52
+ this . handleStop = null ;
46
53
}
47
54
componentDidMount ( ) {
48
55
this . audioEngine = new AudioEngine ( ) ;
@@ -51,10 +58,22 @@ class SoundLibrary extends React.PureComponent {
51
58
componentWillUnmount ( ) {
52
59
this . stopPlayingSound ( ) ;
53
60
}
61
+ onStop ( ) {
62
+ if ( this . playingSoundPromise !== null ) {
63
+ this . playingSoundPromise . then ( soundPlayer => soundPlayer . removeListener ( 'stop' , this . onStop ) ) ;
64
+ if ( this . handleStop ) this . handleStop ( ) ;
65
+ }
66
+
67
+ }
68
+ setStopHandler ( func ) {
69
+ this . handleStop = func ;
70
+ }
54
71
stopPlayingSound ( ) {
55
72
// Playback is queued, playing, or has played recently and finished
56
73
// normally.
57
74
if ( this . playingSoundPromise !== null ) {
75
+ // Forcing sound to stop, so stop listening for sound ending:
76
+ this . playingSoundPromise . then ( soundPlayer => soundPlayer . removeListener ( 'stop' , this . onStop ) ) ;
58
77
// Queued playback began playing before this method.
59
78
if ( this . playingSoundPromise . isPlaying ) {
60
79
// Fetch the player from the promise and stop playback soon.
@@ -102,6 +121,7 @@ class SoundLibrary extends React.PureComponent {
102
121
// Play the sound. Playing the sound will always come before a
103
122
// paired stop if the sound must stop early.
104
123
soundPlayer . play ( ) ;
124
+ soundPlayer . addListener ( 'stop' , this . onStop ) ;
105
125
// Set that the sound is playing. This affects the type of stop
106
126
// instruction given if the sound must stop early.
107
127
if ( this . playingSoundPromise !== null ) {
@@ -144,6 +164,7 @@ class SoundLibrary extends React.PureComponent {
144
164
showPlayButton
145
165
data = { soundLibraryThumbnailData }
146
166
id = "soundLibrary"
167
+ setStopHandler = { this . setStopHandler }
147
168
tags = { soundTags }
148
169
title = { this . props . intl . formatMessage ( messages . libraryTitle ) }
149
170
onItemMouseEnter = { this . handleItemMouseEnter }
0 commit comments