diff --git a/app/src/component/AppNavigator.js b/app/src/component/AppNavigator.js index caf0c36f..1268d405 100644 --- a/app/src/component/AppNavigator.js +++ b/app/src/component/AppNavigator.js @@ -228,7 +228,7 @@ class AppWithNavigationState extends React.Component { } _handleAppStateChange = (nextAppState) => { - const { dispatch } = this.props; + const { backgroundPlayEnabled, dispatch } = this.props; // Check if the app was suspended if (AppState.currentState && AppState.currentState.match(/inactive|background/)) { AsyncStorage.getItem('firstLaunchTime').then(start => { @@ -237,12 +237,21 @@ class AppWithNavigationState extends React.Component { // If so, this needs to be included as a property when tracking AsyncStorage.setItem('firstLaunchSuspended', 'true'); } + + // Background media + if (backgroundPlayEnabled && NativeModules.BackgroundMedia && window.currentMediaInfo) { + const { title, channel } = window.currentMediaInfo; + NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, null, false); + } }); } if (AppState.currentState && AppState.currentState.match(/active/)) { // Cleanup blobs for completed files upon app resume to save space dispatch(doDeleteCompleteBlobs()); + if (backgroundPlayEnabled || NativeModules.BackgroundMedia) { + NativeModules.BackgroundMedia.hidePlaybackNotification(); + } } } @@ -299,6 +308,7 @@ class AppWithNavigationState extends React.Component { } const mapStateToProps = state => ({ + backgroundPlayEnabled: makeSelectClientSetting(SETTINGS.BACKGROUND_PLAY_ENABLED)(state), keepDaemonRunning: makeSelectClientSetting(SETTINGS.KEEP_DAEMON_RUNNING)(state), nav: state.nav, notification: selectNotification(state), diff --git a/app/src/component/mediaPlayer/view.js b/app/src/component/mediaPlayer/view.js index c17b0392..b2db5e51 100644 --- a/app/src/component/mediaPlayer/view.js +++ b/app/src/component/mediaPlayer/view.js @@ -1,6 +1,7 @@ import React from 'react'; import { Lbry } from 'lbry-redux'; import { + DeviceEventEmitter, NativeModules, PanResponder, Text, @@ -225,9 +226,13 @@ class MediaPlayer extends React.PureComponent { componentDidMount() { this.setSeekerPosition(this.calculateSeekerPosition()); + DeviceEventEmitter.addListener('onBackgroundPlayPressed', this.play); + DeviceEventEmitter.addListener('onBackgroundPausePressed', this.pause); } componentWillUnmount() { + DeviceEventEmitter.removeListener('onBackgroundPlayPressed', this.play); + DeviceEventEmitter.removeListener('onBackgroundPausePressed', this.pause); this.clearControlsTimeout(); this.setState({ paused: true, fullscreenMode: false }); const { onFullscreenToggled } = this.props; @@ -236,6 +241,24 @@ class MediaPlayer extends React.PureComponent { } } + play = () => { + this.setState({ paused: false }, this.updateBackgroundMediaNotification); + } + + pause = () => { + this.setState({ paused: true }, this.updateBackgroundMediaNotification); + } + + updateBackgroundMediaNotification = () => { + const { backgroundPlayEnabled } = this.props; + if (backgroundPlayEnabled) { + if (NativeModules.BackgroundMedia && window.currentMediaInfo) { + const { title, channel } = window.currentMediaInfo; + NativeModules.BackgroundMedia.showPlaybackNotification(title, channel, null, this.state.paused); + } + } + } + renderPlayerControls() { if (this.state.areControlsVisible) { return ( @@ -285,7 +308,7 @@ class MediaPlayer extends React.PureComponent { return (