Merge pull request #117 from michaeltintiuc/audio-focus

Fixes #116. Listen to audio focus change
This commit is contained in:
Akinwale Ariwodola 2020-02-04 05:12:15 +01:00 committed by GitHub
commit 3a5edc461a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -167,7 +167,7 @@ class MediaPlayer extends React.PureComponent {
togglePlay = () => { togglePlay = () => {
this.showPlayerControls(); this.showPlayerControls();
this.setState({ paused: !this.state.paused }, this.handlePausedState); this.setState({ paused: !this.state.paused }, this.updateBackgroundMediaNotification);
}; };
handlePausedState = () => { handlePausedState = () => {
@ -188,7 +188,7 @@ class MediaPlayer extends React.PureComponent {
}; };
onEnd = () => { onEnd = () => {
this.setState({ paused: true }); this.setState({ paused: true }, this.updateBackgroundMediaNotification);
if (this.props.onPlaybackFinished) { if (this.props.onPlaybackFinished) {
this.props.onPlaybackFinished(); this.props.onPlaybackFinished();
} }
@ -327,6 +327,10 @@ class MediaPlayer extends React.PureComponent {
} }
}; };
onFocusChanged = evt => {
this.setState({ paused: !(this.state.paused && evt.hasAudioFocus) }, this.updateBackgroundMediaNotification);
};
onBuffer = () => { onBuffer = () => {
if (!this.state.paused) { if (!this.state.paused) {
this.setState({ buffering: true }, () => this.manualHidePlayerControls()); this.setState({ buffering: true }, () => this.manualHidePlayerControls());
@ -469,6 +473,7 @@ class MediaPlayer extends React.PureComponent {
onEnd={this.onEnd} onEnd={this.onEnd}
onError={this.onError} onError={this.onError}
minLoadRetryCount={999} minLoadRetryCount={999}
onAudioFocusChanged={this.onFocusChanged}
/> />
{this.state.firstPlay && thumbnail && ( {this.state.firstPlay && thumbnail && (