Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 298
Tamir Scherzer Avatar asked Apr 03 '16 07:04

Tamir Scherzer


People also ask

How do I stop video playing on Android WebView?

setBuiltInZoomControls(false); webView. getSettings().

How do I stop loading WebView in react-native?

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.

How play Youtube in WebView react-native?

/** * 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={' ...

How can I make WebView keep a video or audio playing in the background?

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.


1 Answers

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.

like image 181
Agu Dondo Avatar answered Sep 20 '22 18:09

Agu Dondo