How to prevent React Native Android Webview from running Youtube in the background?

前端 未结 3 1971
陌清茗
陌清茗 2020-12-11 16:19

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

3条回答
  •  没有蜡笔的小新
    2020-12-11 16:42

    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' &&
               
          }
    
        )
    
      }
    
    }
    

    This is good if you are working with Expo, no need to touch any native code.

提交回复
热议问题