javascript - React JS,HTML音频标签未加载音乐文件
问题描述
使用 create-react-app 我创建了应用程序,浏览器audio
标签未加载 mp3 文件。
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<audio controls src="Kalimba.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
);
}
export default App;
解决方案
不确定是否mp3
会开箱即用,但根据文档的一个选项 可能是:
import React from 'react';
import file from './foobar.mp3'; // Tell webpack this JS script uses this file
console.log(file); // /foobar.84287d09.mp3
function Foobar() {
// Import result is the URL of your file
return (<audio controls src={file}>
Your browser does not support the
<code>audio</code> element.
</audio>);
}
export default Foobar;
另一种方法是将文件放入mp3
文件public
夹,然后:
render() {
// Note: this is an escape hatch and should be used sparingly!
// Normally we recommend using `import` for getting asset URLs
return (<audio controls src={process.env.PUBLIC_URL + '/foobar.mp3'}>
Your browser does not support the
<code>audio</code> element.
</audio>);
}
推荐阅读
- c# - ASP.NetCore3.0 WebApi,带有 CRA 的解决方案将无法在 Visual Studio 中正确加载
- bash - 在执行其他命令时将 ssh 输出重定向到文件
- vue.js - 如何正确使用 vue 路由器上的元道具?
- php - 如何使用 FollowUpBoss Api 获取客户的自定义字段值
- codenameone - 使用 Codename One 管理 http 错误代码
- matlab - 如何将多个点对象存储在数组或结构中?
- c++ - 在没有系统调用(管道)的线程之间使用 std::istream 和 std::ostream
- python - Strange syntax and error when executing lambda function with multiple arguments
- r - 如何以有效的方式根据其他索引变量的条件创建变量?
- python-2.7 - 使用正则表达式获取匹配词的索引