我的应用在 Play 商店中被拒绝,原因是“确保它不会启用 YouTube 视频的后台播放”。我使用了基本的 Webview 组件并加载了一个 youtube 页面。播放完 youtube 电影并返回主页后,youtube 电影继续在后台播放。
当应用程序暂停时,我如何获得暂停 Webview 的句柄?
最佳答案
您可以使用来自 react-native 的 AppState:documentation .
并且仅当应用状态为“ Activity ”时显示 WebView (其中包含您的 YouTube 嵌入内容)
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 />
}
)
}
}
如果您正在使用 Expo,这很好,无需接触任何 native 代码。
关于android - 如何防止 React Native Android Webview 在后台运行 Youtube?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36382935/