react-native-video使用

react-native APP 需要播放视频功能,这里选用 react-native-video 库,这个库在 GitHub 上有 3k+ 的星。

简单使用

import Video from 'react-native-video';
import * as _ from 'lodash';

class Introduction extends React.Component<IIntroductionProps, any> {
  static navigationOptions = ({ navigation }: any) => ({
    title: "title",
  });

  player: any;
  constructor(props: IIntroductionProps) {
    super(props);
    this.state = {
      videoWidth: 0,
      videoHeight: 0,
      playerIconVisible: true,
      paused: true,
      playableDuration: 0,
      currentTime: 0
    };
  }

  render() {
    return (
      <View style={{ flex: 1, backgroundColor: "#fff" }}>
        <Video
          style={{
            flex: 1,
            width: this.state.videoWidth,
            height: this.state.videoHeight
          }}
          source={{ uri: "http://cdn.xxx.com/videos/xxx.mp4" }} // Can be a URL or a local file.
          ref={(ref: any) => (this.player = ref)}
          paused={this.state.paused}
          onEnd={() => {}}
          onError={() => {}}
          onLoad={(e: any) => {
            // console.log('onLoad: ', e);
            this.setState({ playableDuration: _.get(e, "duration", 0) });
          }}
          onProgress={(e: any) => {
            this.setState({ currentTime: _.get(e, "currentTime", 0) });
          }}
          // onFullscreenPlayerDidPresent={() => {}}
        />
      </View>
    );
  }
}

编译问题

安卓编译报错 1:

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApk'.
> A problem occurred configuring project ':react-native-video'.
> Could not resolve all dependencies for configuration ':react-native-video:_debugPublishCopy'.
> Could not find com.android.support:support-annotations:27.0.0.
Searched in the following locations:
file:/Users/wwpeng520/Library/Android/sdk/extras/android/m2repository/com/android/support/support-annotations/27.0.0/support-annotations-27.0.0.pom

解决方案:android/build.gradle 中加上 maven { url 'https://maven.google.com' },如下所示:

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

安卓编译报错 2:

Dex: The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)
// ...其他信息
:app:transformClassesWithDexForDebug FAILED
FAILURE: Build failed with an exception. * What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

解决方案:android/app/build.gradle 中 defaultConfig 加上 multiDexEnabled = true

安卓编译报错 3:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

这个编译错误是因为引入过多的第三方 jar 包导致的。在编译文件 build.gradle 中 android 下加入如下代码,即可解决。

dexOptions{
    javaMaxHeapSize "4g"
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,822评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,143评论 2 59
  • 怎么快速祛斑?去斑最有效的方法帮你解决斑点难题。脸蛋是女人的第一扇门,是女人的第一面镜子,脸上长斑是谁都不愿意看到...
    幸运草_7a7a阅读 1,022评论 0 0
  • 文/郑小满 现在似乎很流行做“斜杠青年”。 斜杠青年,顾名思义,指的是这样一群人:他们不满足于单一的职业和身份的束...
    小满说阅读 688评论 0 3
  • 不知道为什么,总是特别的喜欢冬天,从我记得事情开始就喜欢冬天,在秋天走过了之后,我们就迎来了美丽的冬天,我觉得冬天...
    牛头马阅读 209评论 0 0