My app was rejected in play store for "make sure it doesn't enable background play of YouTube videos". I used the basic Webview component and loaded a youtube page. After playing the youtube movie, and going back to home page, the youtube movie keeps on playing in the background.
How do I get a handle to pause the Webview when the app gets paused?
setBuiltInZoomControls(false); webView. getSettings().
You don't want to stop loading the page before most of the items on it have fully loaded. If you're trying to stop a running script on the page, you should look at injecting JavaScript into the WebView to stop the script from continuing.
/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */ import React from 'react'; import {View} from 'react-native'; import YoutubePlayer from 'react-native-youtube-iframe'; const App = () => { return ( <View> <YoutubePlayer height={300} play={true} videoId={' ...
Solution. After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened.
You could use AppState from react-native: documentation.
And show the webview (which contains your YouTube embed) only if the app state is 'active'
import React, {Component} from 'react'
import {AppState, WebView} from 'react-native'
class AppStateExample extends Component {
state = {
appState: AppState.currentState
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
this.setState({appState: nextAppState});
}
render() {
return (
{this.state.appState == 'active' &&
<WebView />
}
)
}
}
This is good if you are working with Expo, no need to touch any native code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With