将n个react-native-videos放在一个呈现react-native-render-html的滚动视图中

bakd9h0s  于 2023-10-22  发布在  React
关注(0)|答案(1)|浏览(113)

我如何把react-native-video的一个接一个地在一个滚动视图,渲染html使用react-native-render-html我已经尝试过,但问题是他们得到放置在对方的顶部,如果我试图放置另一个html元素比我看不到它。

This is my attempt

import React from "react";
import { View, ScrollView, StyleSheet } from "react-native";
import Video from "react-native-video";
import RenderHTML, {
  HTMLContentModel,
  HTMLElementModel,
} from "react-native-render-html";
import { SCREEN_WIDTH } from "../components/lib/constants";

function RenderHtml({ uri }: { uri: string }) {
  return (
    <>
      <RenderHTML
        contentWidth={SCREEN_WIDTH}
        source={{ html: uri }}
        customHTMLElementModels={{
          video: HTMLElementModel.fromCustomModel({
            tagName: "video",
            mixedUAStyles: {
              alignSelf: "center",
            },
            contentModel: HTMLContentModel.block,
          }),
        }}
        renderers={{
          video: (obj1: any, obj2: any) => {
            let link = obj1["tnode"].domNode.attribs.src;
            return (
              <>
                <Video
                  source={{ uri: link }}
                  resizeMode={"contain"}
                  style={[{ width: SCREEN_WIDTH, height: 300, flex: 1 }]}
                  controls={true}
                  paused={true}
                />
              </>
            );
          },
        }}
      />
    </>
  );
}
// test video uri from expo-video example
export default function Test() {
  return (
    <ScrollView style={styles.container}>
      <RenderHtml
        uri={
          "<video src='https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4' /><video src='https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4' />"
        }
      />
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "black",
  },
  videoContainer: {
    flex: 1,
    height: 300, // Adjust the height as needed to fit your videos
    justifyContent: "center",
    alignItems: "center",
  },
  video: {
    width: "100%",
    height: "100%",
  },
});
sd2nnvve

sd2nnvve1#

问题是你没有在视频标签中使用右括号,所以将<video ... />更改为<video ...></video

import React from "react";
import { View, ScrollView, StyleSheet } from "react-native";
import Video from "react-native-video";
import RenderHTML, {
  HTMLContentModel,
  HTMLElementModel,
} from "react-native-render-html";
import { SCREEN_HEIGHT, SCREEN_WIDTH } from "../components/lib/constants";

function RenderHtml({ uri }: { uri: string }) {
  return (
    <RenderHTML
      contentWidth={SCREEN_WIDTH}
      source={{ html: uri }}
      customHTMLElementModels={{
        video: HTMLElementModel.fromCustomModel({
          tagName: "video",
          mixedUAStyles: {
            alignSelf: "center",
          },
          contentModel: HTMLContentModel.block,
        }),
      }}
      renderers={{
        video: (obj1: any, obj2: any) => {
          let link = obj1["tnode"].domNode.attribs.src;
          return (
            <View style={styles.videoContainer}>
              <Video
                source={{ uri: link }}
                resizeMode={"contain"}
                style={styles.video}
                controls={true}
                paused={true}
              />
            </View>
          );
        },
      }}
    />
  );
}

export default function Test() {
  return (
    <ScrollView style={styles.container}>
      <RenderHtml
        uri={
          "<video src='https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4'></video><video src='https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4'></video>"
        }
      />
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  container: {
    minWidth: SCREEN_WIDTH,
    minHeight: SCREEN_HEIGHT,
    backgroundColor: "black",
  },
  videoContainer: {
    width: SCREEN_WIDTH, // Display videos side by side, adjust as needed
    height: 300, // Adjust the height as needed to fit your videos
    justifyContent: "center",
    alignItems: "center",
  },
  video: {
    width: "100%",
    height: "100%",
  },
});

相关问题