React Native 视频在我的应用中显示空白屏幕,没有任何错误日志。

4

我正在使用react-native-cli,但是在我的应用程序中,react-native-video无法在我的页面上工作。它只显示了空白的位置。我已经运行了react-native link命令以链接库,并在运行react-native run-android命令后,但是页面仍然显示空白而没有任何错误。我正在使用React Native版本0.48.4。感谢您的帮助!!

import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView,Image, Dimensions,Alert } from 'react-native';
import Video from 'react-native-video';


export default class HomeScreen extends Component {
  constructor(props) {
    super(props);

    this.loadStart = this.loadStart.bind(this);
    this.onLoad = this.onLoad.bind(this);
    this.onProgress = this.onProgress.bind(this);
    this.onEnd = this.onEnd.bind(this);
    this.onError = this.onError.bind(this);
    this.onBuffer = this.onBuffer.bind(this);
    this.onTimedMetadata = this.onTimedMetadata.bind(this);
  };

  loadStart(){
    console.log('loadStart');
  }
  onLoad(){
    console.log('onLoad');
  }

  onProgress(){
    console.log('onProgress');
  }

  onEnd(){
    console.log('onEnd');
  }

  onError(){
    console.log('onError');
  }

  onBuffer(){
    console.log('onBuffer');
  }

  onTimedMetadata(){
    console.log('onTimedMetadata');
  }

  render() {
    return (


        <View style={styles.container}>
          <Image style={styles.logo} source={require('../../images/logo.png')} />
          <View style={styles.Body}>
            <ScrollView>
              <View style={styles.scrollerInner}>
                <Video source={require('../../images/tndc-video.mp4')}   // Can be a URL {uri:'https://www.w3schools.com/html/mov_bbb.mp4'} or a local file require('').   
                  ref={(ref) => {this.player = ref}}               
                  muted={false}                           // Mutes the audio entirely.                  
                  resizeMode="cover"                      // Fill the whole screen at aspect ratio.*
                  repeat={false}                           // Repeat forever.
                  playInBackground={false}                // Audio continues to play when app entering background.
                  playWhenInactive={false}                // [iOS] Video continues to play when control or notification center are shown.
                  ignoreSilentSwitch={"ignore"}           // [iOS] ignore | obey - When 'ignore', audio will still play with the iOS hard silent switch set to silent. When 'obey', audio will toggle with the switch. When not specified, will inherit audio settings as usual.
                  progressUpdateInterval={250.0}          // [iOS] Interval to fire onProgress (default to ~250ms)
                  onLoadStart={this.loadStart}            // Callback when video starts to load
                  onLoad={this.setDuration}               // Callback when video loads
                  onProgress={this.setTime}               // Callback every ~250ms with currentTime
                  onEnd={this.onEnd}                      // Callback when playback finishes
                  onError={this.videoError}               // Callback when video cannot be loaded
                  onBuffer={this.onBuffer}                // Callback when remote video is buffering
                  onTimedMetadata={this.onTimedMetadata}  // Callback when the stream receive some metadata
                  style={styles.videoPlayer} 
                />                
              </View>
            </ScrollView> 
          </View>
        </View> 


    );
  }
}    

const styles = StyleSheet.create({
  container: {
    flex:1,
    paddingTop:30,
    width:'100%',
  },
  logo:{
    width:260,
    height:66,
    marginBottom:20,
    marginLeft:20,
  },
  Body:{
    width:'100%',
    flexGrow:1,
    height:30
  },
  scrollerInner:{
    paddingHorizontal:20,
  },
  title:{
    fontSize:30,
    color:'#000',
    fontWeight:'bold',
    marginBottom:12,
  },
  description:{
    fontSize:16,
    color:'#000',
    marginBottom:20,
  },
  videoPlayer:{
    width:Dimensions.get('window').width,
    backgroundColor:'#000',
    height:300,
  }, 
});

请尝试将 styles.videoPlayerposition: 'absolute' 属性添加进去。 - bennygenel
尝试使用“absolute”定位,但不起作用。 - Rajnikant Kakadiya
一个白色屏幕,带有红色边框?:) - Moji Izadmehr
不,只是显示空白空间。 - Rajnikant Kakadiya
1个回答

5
我认为你的主要问题在于文件位置。使用 require('../../images/tndc-video.mp4') 时,你正在项目文件夹之外查找文件。在我的最近一个项目中,我尝试为其他js文件做到这一点。你可以通过在webpack配置中添加其他文件夹来实现,但是React打包器不喜欢这样做,而且它也不是很稳定。因此,你的“快速修复”方法是将资源放在项目文件夹内,如require('./images/tndc-video.mp4')其他发现 在解决这个问题时,我发现了一种奇怪的“require”工作方式。最初,我以为整个问题都是关于本地资产的捆绑问题,但实际上它与文件名有关。
使用你的代码,我得到了一个黑屏和以下源:

source={require('./assets/google_pixel_2_impressions.mp4')}

iOS模拟器截图带有文件名空格 我不能嵌入图片,抱歉。
但是当我将文件名更改为使用下划线而不是空格时,它就正常了。

source={require('./assets/google_pixel_2_impressions.mp4')}

文件名中带下划线的iOS模拟器截图

我认为这可能会有所帮助。

容器源代码。我使用了您的样式和辅助函数。

    <Image style={styles.logo} source={require('./assets/so-logo.png')}//source={{uri:"https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png"}} 
    />

    <View style={styles.Body}>

    <ScrollView>
    <View style={styles.scrollerInner}>

        <Video 
            // source={require('../images/mov_bbb.mp4')}
            source={require('./assets/google_pixel_2_impressions.mp4')}
            // source={require('./assets/google pixel 2 impressions.mp4')}

            ref={(ref) => {this.player = ref}}               
            muted={false}                           
            resizeMode="cover"                      
            repeat={false}                          
            playInBackground={false}                
            playWhenInactive={false}                
            ignoreSilentSwitch={"ignore"}           
            progressUpdateInterval={250.0}          
            onLoadStart={this.loadStart}            
            onLoad={this.setDuration}               
            onProgress={this.setTime}               
            onEnd={this.onEnd}                      
            onError={this.videoError}               
            onBuffer={this.onBuffer}                
            onTimedMetadata={this.onTimedMetadata}  
            style={styles.videoPlayer} 
        />

    </View>
    </ScrollView>

    </View>
</View>

使用全新的react-native init项目进行测试,版本为react: 16.0.0-beta.5, react-native: 0.49.1,react-native-video: 2.0.0。


谢谢你的帮助,Dennis!我已经尝试将资产文件夹放在我的项目根目录,并按照你的建议给出了路径,但不幸的是它仍然无法工作 :( 在安卓上显示为空白空间,没有错误。 - Rajnikant Kakadiya
你尝试过使用URI进行测试吗?看看播放器是否能正常工作。 source={{uri:'https://www.w3schools.com/html/mov_bbb.mp4'}}如果你在Windows上,需要在react-native-video运行之前执行额外的步骤。如果你在Mac上,iOS模拟器输出是什么? - Dennis Tsoi
是的,尝试过使用{{uri:'https://www.w3schools.com/html/mov_bbb.mp4'}},但是仍然是空白屏幕。 - Rajnikant Kakadiya
我们知道问题出在播放器设置上,而不是所需或文件名。我尝试在Genymotion中使用相同的代码,它可以正常工作。您能否编辑帖子以显示以下文件的内容?android/settings.gradle;android/app/build.gradle;android/app/src/main/java/com/PRJNAME/MainApplication.java - Dennis Tsoi
我也尝试使用uri作为源{uri:'https://www.w3schools.com/html/mov_bbb.mp4'},它显示了一个白屏大约一分钟左右,但随后视频正常播放。 - Dennis Tsoi
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接