2017-10-28 03:29:48 +08:00
|
|
|
import React from 'react';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
play: PropTypes.bool.isRequired,
|
|
|
|
count: PropTypes.number.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChatAudioNotification extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-05-08 07:15:50 +08:00
|
|
|
this.audio = new Audio(`${Meteor.settings.public.app.basename}/resources/sounds/notify.mp3`);
|
2017-10-28 03:29:48 +08:00
|
|
|
|
|
|
|
this.handleAudioLoaded = this.handleAudioLoaded.bind(this);
|
|
|
|
this.playAudio = this.playAudio.bind(this);
|
|
|
|
this.componentDidUpdate = _.debounce(this.playAudio, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.audio.addEventListener('loadedmetadata', this.handleAudioLoaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
return nextProps.count > this.props.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.audio.removeEventListener('loadedmetadata', this.handleAudioLoaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAudioLoaded() {
|
|
|
|
this.componentDidUpdate = _.debounce(this.playAudio, this.audio.duration * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
playAudio() {
|
|
|
|
if (!this.props.play) return;
|
|
|
|
|
|
|
|
this.audio.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ChatAudioNotification.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default ChatAudioNotification;
|