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

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

问题描述

我的应用在 Play 商店中被拒绝,原因是确保它没有启用 YouTube 视频的后台播放".我使用了基本的 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.

仅当应用状态为活动"时才显示网络视图(其中包含您的 YouTube 嵌入)

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天全站免登陆