首页 > 解决方案 > 如何在本机反应中呈现html音频标签?

问题描述

为什么 react-native-html-render 不渲染 html 音频标签?这个库可以做到吗?由于有 iframe 插件来处理视频,感觉应该有一个插件来处理音频,但我找不到。提前致谢。

标签: reactjsreact-nativepluginsreact-native-render-htmlrender-html

解决方案


您还可以使用expo-av库在您的应用程序中播放音频 ( https://docs.expo.dev/versions/latest/sdk/audio/ )

import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { Audio } from 'expo-av';

export default function App() {
  const [sound, setSound] = React.useState();

  async function playSound() {
    console.log('Loading Sound');
    const { sound } = await Audio.Sound.createAsync(
       require('./assets/Hello.mp3')
    );
    setSound(sound);

    console.log('Playing Sound');
    await sound.playAsync(); }

  React.useEffect(() => {
    return sound
      ? () => {
          console.log('Unloading Sound');
          sound.unloadAsync(); }
      : undefined;
  }, [sound]);

  return (
    <View style={styles.container}>
      <Button title="Play Sound" onPress={playSound} />
    </View>
  );
}

推荐阅读