Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在WebView中控制播放/暂停视频_React Native_Webview_Html5 Video - Fatal编程技术网

React native 如何在WebView中控制播放/暂停视频

React native 如何在WebView中控制播放/暂停视频,react-native,webview,html5-video,React Native,Webview,Html5 Video,我正试图通过React Native在WebView中播放和暂停视频。 但是当this.state.shouldPlay变为true时,下面的injectedJavaScript就不起作用了 请建议如何从React Native控制WebView中的视频。 由于性能问题,无法使用react本机视频 import * as React from 'react'; import { Component, useState, useEffect } from 'react'; import { Te

我正试图通过React Native在WebView中播放和暂停视频。
但是当this.state.shouldPlay变为true时,下面的injectedJavaScript就不起作用了

请建议如何从React Native控制WebView中的视频。
由于性能问题,无法使用react本机视频

import * as React from 'react';
import { Component, useState, useEffect  } from 'react';
import { Text, View, StyleSheet, Image, TouchableOpacity} from 'react-native';
import {WebView} from 'react-native-webview';

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      shouldPlay: false, // not play at default
    }
  }

  _handlePlayAndPause = () => {
    if (this.state.shouldPlay == false) {
      this.setState({shouldPlay: true });
    } else {
      this.setState({shouldPlay: false });
    };

  render() {
    const {shouldPlay } = this.state;

    return (
        <View style={styles.container}>
          { shouldPlay ? 
            <View>
              <WebView
                source={{ uri: www.url.com/sample.mp4 }}
                javaScriptEnabled = {true}
                injectedJavaScript=
                  {`document.getElementsByTagName("video")[0].play();`}
              />
            </View>
          :
            <View>
              <WebView
                source={{ uri: www.url.com/sample.mp4 }}
                javaScriptEnabled = {true}
                injectedJavaScript=
                  {`document.getElementsByTagName("video")[0].pause();`}
              />
            </View>
          }

          { shouldPlay ?
            <View>
              <TouchableOpacity onPress={ this._handlePlayAndPause } >  
                  <Ionicons name="ios-pause"/>
              </TouchableOpacity>
            </View>
          :
            <View>
              <TouchableOpacity onPress={ this._handlePlayAndPause } > 
                  <Ionicons name="ios-play-circle"/>
              </TouchableOpacity>
            </View>              
          }
        </View>
    )  
  }

}
import*as React from'React';
从“react”导入{Component,useState,useffect};
从“react native”导入{文本、视图、样式表、图像、TouchableOpacity};
从“react native WebView”导入{WebView};
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
shouldPlay:false,//默认情况下不播放
}
}
_handlePlayAndPause=()=>{
if(this.state.shouldPlay==false){
this.setState({shouldlplay:true});
}否则{
this.setState({shouldlplay:false});
};
render(){
const{shouldlplay}=this.state;
返回(
{应该玩吗?
:
}
{应该玩吗?
:
}
)  
}
}

谢谢。

我遇到同样的问题,你能找到解决办法吗?
import * as React from 'react';
import { Component, useState, useEffect  } from 'react';
import { Text, View, StyleSheet, Image, TouchableOpacity} from 'react-native';
import {WebView} from 'react-native-webview';

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      shouldPlay: false, // not play at default
    }
  }

  _handlePlayAndPause = () => {
    if (this.state.shouldPlay == false) {
      this.setState({shouldPlay: true });
    } else {
      this.setState({shouldPlay: false });
    };

  render() {
    const {shouldPlay } = this.state;

    return (
        <View style={styles.container}>
          { shouldPlay ? 
            <View>
              <WebView
                source={{ uri: www.url.com/sample.mp4 }}
                javaScriptEnabled = {true}
                injectedJavaScript=
                  {`document.getElementsByTagName("video")[0].play();`}
              />
            </View>
          :
            <View>
              <WebView
                source={{ uri: www.url.com/sample.mp4 }}
                javaScriptEnabled = {true}
                injectedJavaScript=
                  {`document.getElementsByTagName("video")[0].pause();`}
              />
            </View>
          }

          { shouldPlay ?
            <View>
              <TouchableOpacity onPress={ this._handlePlayAndPause } >  
                  <Ionicons name="ios-pause"/>
              </TouchableOpacity>
            </View>
          :
            <View>
              <TouchableOpacity onPress={ this._handlePlayAndPause } > 
                  <Ionicons name="ios-play-circle"/>
              </TouchableOpacity>
            </View>              
          }
        </View>
    )  
  }

}