如何防止React Native Android Webview在后台运行Youtube? [英] How to prevent React Native Android Webview from running Youtube in the background?

查看:60
本文介绍了如何防止React Native Android Webview在后台运行Youtube?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用因确保它不启用YouTube视频的后台播放"而在Play商店中被拒绝.我使用了基本的Webview组件并加载了youtube页面.播放完youtube电影后,返回首页,该youtube电影将继续在后台播放.

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.

当应用程序暂停时,如何获得暂停Webview的句柄?

How do I get a handle to pause the Webview when the app gets paused?

推荐答案

您可以在react-native中使用 AppState :

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 />
      }

    )

  }

}

如果您正在使用 Expo ,这很好,无需触摸任何本机代码.

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

这篇关于如何防止React Native Android Webview在后台运行Youtube?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆