android - 使用 React Native 的 Webview 和 listview 中的 Youtube 视频

标签 android listview react-native webview youtube

我是 React Native 的新手。我面临 Youtube 视频仅播放音频的问题。下面是我现在使用的代码:

import React, { Component } from 'react'

import {
   View,
   ListView,
   WebView,
   StyleSheet,
   ScrollView
}
from 'react-native'

 url=[
    {
    "videourl":"https://www.youtube.com/embed/ZnvJvZAJZPk",
    },
    {
    "videourl":"https://www.youtube.com/embed/HOI8nlFa86U",
    },
    {
    "videourl":"https://www.youtube.com/embed/0bDI_rAMV68",
    },
    ]

class WebViewExample extends Component{

  constructor (props) {
    super (props);

    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2
    });

    this.state = {
      dataSource: ds.cloneWithRows(url),
    };
  }

  render(){
    return (
      <ListView
        dataSource={ this.state.dataSource }
        renderRow={this.renderRows.bind(this)}
      />
    );
  }
  renderRows(rowData){
    console.log("url: "+rowData.videourl);
    return(
      <View style={styles.container}>
        <View style={{borderWidth:3, borderColor : '#FF0000', flex:1}}>
          <WebView source={{uri: rowData.videourl}} style={{}} />
        </View>
      </View>
    );
  }
}
export default WebViewExample;

const styles = StyleSheet.create({
   container: {
      height: 350,
   }
})

React 原生版本:0.47

最佳答案

虽然代码是正确的,但它会抛出错误,因为您必须将 url 作为 const

const url=[
    {
    "videourl":"https://www.youtube.com/embed/ZnvJvZAJZPk",
    },
    {
    "videourl":"https://www.youtube.com/embed/HOI8nlFa86U",
    },
    {
    "videourl":"https://www.youtube.com/embed/0bDI_rAMV68",
    },
    ];

检查 Expo

注意:视频不会在 Expo Web 版本中显示,但您可以在移动版本中看到它。

关于android - 使用 React Native 的 Webview 和 listview 中的 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46173403/

相关文章:

c# ListView :How to create Columns without header

react-native - 我们如何将 iOS 和 Android 项目隔离到他们自己的仓库中,但维护一个 React-native 仓库?

android - 如何使用数组改造解析 JSON 对象

Android Fragment 返回堆栈

Android ListView重复修改列表项

listview - React Native - 当数据未更改时强制 ListView 重新渲染

javascript - react native 嵌套堆栈导航显示空屏幕

react-native - 在 FlatList 中添加按钮 "see more"?

android - Facebook SDK 与 Android Studio 集成

android - 如何确保android使用默认字体?