reactjs - 如何将 mp3 文件添加到反应 js 文件中
问题描述
我正在尝试为我的 react js 应用程序播放单个 mp3 文件。但是,当我向我的方法添加文件路径时,它无法识别它。mp3 位于应用程序内。
我将路径放在变量中,然后将变量放入 html 音频元素中。
如何让我的 mp3 在我的文件中播放?
import React, { useState,useEffect } from "react";
const moby = require('../music/mobyPorcelain.mp3')
const Sound = new Audio(moby)
export default function Header() {
const [playInLoop, setPlayInLoop] = useState(false);
useEffect(()=> {
Sound.load();
}, [])
useEffect(() => {
Sound.loop = playInLoop
},[playInLoop])
const playSound = () => {
Sound.play();
}
const pauseSound = () => {
Sound.pause();
}
const stopSound = () => {
Sound.pause();
Sound.currentTime = 0;
}
return (
<div>
<h3>Moby porcelain</h3>
<input
type='button'
className='btn btn-primary mr-2'
value='play'
onclick={playSound}/>
<input
type='button'
className='btn btn-warning mr-2'
value='pause'
onclick={pauseSound}/>
<input
type='button'
className='btn btn-danger mr-3'
value='stop'
onclick={stopSound}/>
<label><input type='checkbox' checked={playInLoop} onChange={e => setPlayInLoop(e.target.checked)}/></label>
</div>
);
}
解决方案
推荐阅读
- ios - 将 main.strings 文件中的现有字符串用于另一个元素的文本
- java - 在正确的包结构中找不到没有@EnableJpaRepositories 的存储库
- javascript - 如何集成更少的代码行?
- vue.js - 如何让全球商店与 Nuxt 中的其他商店共享
- css - 当我只对文本应用填充时,为什么图像和文本都会移动?
- python - django.core.exceptions.FieldError:ModelForm 中的未知字段
- javascript - 如何在 MarkLogic 上的 Corb2 中增加堆大小
- scala - 如何在 Spark Scala 中运行批量配置单元查询
- reactjs - 我可以使用 UseState 在两个类别之间切换数据吗?
- azure - 使用逻辑应用中可用的函数从字符串中删除前导零