现象:vue项目中引入pcm文件失败
- 报错内容
Module parse failed: Unexpected character ' ' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
解决办法:在webpack中配置中加上pcm格式
module/rules
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac|pcm)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'media/[name]--[folder].[ext]'
}
},
pcm转wav
pcm2wav.js
const getWebFileArrayBuffer = async (url) => {
console.log('url.',url)
// let array = await fetch(url).then(response => response.arrayBuffer())
// return array
var req = new XMLHttpRequest()
req.open("GET", url, true) // grab our audio file
req.responseType = "arraybuffer" // needs to be specific type to work
req.overrideMimeType('text/xml; charset = utf-8')
req.send()
return new Promise((resolve) => {
req.onload = function () {
if (this.status !== 200) {
console.log("pcm文件不存在/文件格式错误!")
}
resolve(req.response)
}