Skip to content

Commit 41cc11c

Browse files
committed
Handle sound ending
Update UI play/stop button when the sound ends
1 parent 6216e7d commit 41cc11c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/components/library/library.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LibraryComponent extends React.Component {
3838
'handleFilterClear',
3939
'handleMouseEnter',
4040
'handleMouseLeave',
41+
'handlePlayingEnd',
4142
'handleSelect',
4243
'handleTagClick',
4344
'setFilteredDataRef'
@@ -54,6 +55,7 @@ class LibraryComponent extends React.Component {
5455
setTimeout(() => {
5556
this.setState({loaded: true});
5657
});
58+
if (this.props.setStopHandler) this.props.setStopHandler(this.handlePlayingEnd);
5759
}
5860
componentDidUpdate (prevProps, prevState) {
5961
if (prevState.filterQuery !== this.state.filterQuery ||
@@ -75,7 +77,6 @@ class LibraryComponent extends React.Component {
7577
});
7678
}
7779
handleMouseEnter (id) {
78-
console.log('Library MouseEnter id:', id, ', playingItem: ', this.state.playingItem);
7980
// don't restart if mouse over already playing item
8081
if (this.props.onItemMouseEnter && this.state.playingItem !== id) {
8182
this.setState({
@@ -90,6 +91,13 @@ class LibraryComponent extends React.Component {
9091
}, this.props.onItemMouseLeave(this.getFilteredData()[id]));
9192
}
9293
}
94+
handlePlayingEnd () {
95+
if (this.state.playingItem !== null) {
96+
this.setState({
97+
playingItem: null
98+
});
99+
}
100+
}
93101
handleFilterChange (event) {
94102
this.setState({
95103
filterQuery: event.target.value,
@@ -239,6 +247,7 @@ LibraryComponent.propTypes = {
239247
onItemMouseLeave: PropTypes.func,
240248
onItemSelected: PropTypes.func,
241249
onRequestClose: PropTypes.func,
250+
setStopHandler: PropTypes.func,
242251
showPlayButton: PropTypes.bool,
243252
tags: PropTypes.arrayOf(PropTypes.shape(TagButton.propTypes)),
244253
title: PropTypes.string.isRequired

src/containers/sound-library.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class SoundLibrary extends React.PureComponent {
2929
bindAll(this, [
3030
'handleItemSelected',
3131
'handleItemMouseEnter',
32-
'handleItemMouseLeave'
32+
'handleItemMouseLeave',
33+
'onStop',
34+
'setStopHandler'
3335
]);
3436

3537
/**
@@ -43,6 +45,11 @@ class SoundLibrary extends React.PureComponent {
4345
* @type {Promise<SoundPlayer>}
4446
*/
4547
this.playingSoundPromise = null;
48+
49+
/**
50+
* function to call when the sound ends
51+
*/
52+
this.handleStop = null;
4653
}
4754
componentDidMount () {
4855
this.audioEngine = new AudioEngine();
@@ -51,10 +58,22 @@ class SoundLibrary extends React.PureComponent {
5158
componentWillUnmount () {
5259
this.stopPlayingSound();
5360
}
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+
}
5471
stopPlayingSound () {
5572
// Playback is queued, playing, or has played recently and finished
5673
// normally.
5774
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));
5877
// Queued playback began playing before this method.
5978
if (this.playingSoundPromise.isPlaying) {
6079
// Fetch the player from the promise and stop playback soon.
@@ -102,6 +121,7 @@ class SoundLibrary extends React.PureComponent {
102121
// Play the sound. Playing the sound will always come before a
103122
// paired stop if the sound must stop early.
104123
soundPlayer.play();
124+
soundPlayer.addListener('stop', this.onStop);
105125
// Set that the sound is playing. This affects the type of stop
106126
// instruction given if the sound must stop early.
107127
if (this.playingSoundPromise !== null) {
@@ -144,6 +164,7 @@ class SoundLibrary extends React.PureComponent {
144164
showPlayButton
145165
data={soundLibraryThumbnailData}
146166
id="soundLibrary"
167+
setStopHandler={this.setStopHandler}
147168
tags={soundTags}
148169
title={this.props.intl.formatMessage(messages.libraryTitle)}
149170
onItemMouseEnter={this.handleItemMouseEnter}

0 commit comments

Comments
 (0)