无法停止反应原生视频

时间:2017-08-29 06:48:10

标签: react-native

我想播放一个音频文件,但是它会自动播放,我无法停止播放。

我该如何解决? 必须在开始时暂停 我的代码:

import Video from 'react-native-video';

export default class Android extends Component {
  constructor(props) {
    super(props)
    this.state = {
      paused: true,
    }
  }

  video: Video;

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
        <Video
          ref={(ref: Video) => { this.video = ref }}
          source={{ uri: "http://s3.picofile.com/d/7376893331/8b7bc5b4-4b5e-47c4-96dd-b0c13fd18157/Sara_Diba_Delbare_Man.mp3", mainVer: 1, patchVer: 0 }}
          paused={this.state.paused}
        />
      </View>
    );
  }
}

2 个答案:

答案 0 :(得分:4)

react-native-video目前存在一个错误,首次加载组件时会忽略pause标志。您必须在组件加载后更改pause

首先,确保您的this.state.pause = false。然后:

<Video
  paused={this.state.paused}
  onLoad={() => {
    this.setState({
      paused: true
    });
  }}
</Video>

背景:https://github.com/react-native-community/react-native-video/issues/494#issuecomment-281853423

答案 1 :(得分:0)

使用ref属性创建视频链接,使用该引用,我们可以在视频组件上使用视频控件

尝试此代码,

import React from "react";
class VideoDemo extends React.Component {
  
  getVideo = elem => {
    this.video = elem
  }

  playVideo = () => {
    // You can use the play method as normal on your video ref
    this.video.play()
  };

  pauseVideo = () => {
    // Pause as well
    this.video.pause();
  };

  render = () => {
    return (
      <div>
        <video
          ref={this.getVideo}
          src="http://techslides.com/demos/sample-videos/small.mp4"
          type="video/mp4"
        />

        <div>
          <button onClick={this.playVideo}>
            Play!
          </button>
          <button onClick={this.pauseVideo}>
            Pause!
          </button>
        </div>
      </div>
    );
  };
}

export default VideoDemo;