播放base64音频
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="file" id="fileInput">
<audio src="" style="
position: fixed;
top: 0;
right: 0;
" controls="" autoplay=""></audio>
<script>
document.querySelector('audio').src=`data:audio/wav;base64,base64val`
</script>
</body>
</html>
音频文件转base64
<input type="file" id="fileInput">
<script>
var fileInput = document.querySelector('#fileInput');
fileInput.onchange = function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
var data = reader.result;
console.log('data', data);
};
};
</script>
base64转音频文件
<script>
const dataurl="****";
const url = URL.createObjectURL(dataURLtoBlob(dataurl));
//接着把url赋值给audio的src。
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
};
</script>