react-native-video使用

react-native APP 需要播放視頻功能,這里選用 react-native-video 庫(kù),這個(gè)庫(kù)在 GitHub 上有 3k+ 的星。

簡(jiǎn)單使用

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>
    );
  }
}

編譯問題

安卓編譯報(bào)錯(cuò) 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"
        }
    }
}

安卓編譯報(bào)錯(cuò) 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。

安卓編譯報(bào)錯(cuò) 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

這個(gè)編譯錯(cuò)誤是因?yàn)橐脒^(guò)多的第三方 jar 包導(dǎo)致的。在編譯文件 build.gradle 中 android 下加入如下代碼,即可解決。

dexOptions{
    javaMaxHeapSize "4g"
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,058評(píng)論 25 709
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,000評(píng)論 2 59
  • 怎么快速祛斑?去斑最有效的方法幫你解決斑點(diǎn)難題。臉蛋是女人的第一扇門,是女人的第一面鏡子,臉上長(zhǎng)斑是誰(shuí)都不愿意看到...
    幸運(yùn)草_7a7a閱讀 1,074評(píng)論 0 0
  • 文/鄭小滿 現(xiàn)在似乎很流行做“斜杠青年”。 斜杠青年,顧名思義,指的是這樣一群人:他們不滿足于單一的職業(yè)和身份的束...
    小滿說(shuō)閱讀 763評(píng)論 0 3
  • 不知道為什么,總是特別的喜歡冬天,從我記得事情開始就喜歡冬天,在秋天走過(guò)了之后,我們就迎來(lái)了美麗的冬天,我覺得冬天...
    牛頭馬閱讀 238評(píng)論 0 0

友情鏈接更多精彩內(nèi)容