javascript - 无法从移动 Chrome 中的 blob url 读取音频
问题描述
我想在我的移动网络应用程序中使用 mp3 播放,所以我编写了一个测试应用程序,我使用了这个解决方案,但是当文件被正确读取时,blob URL 似乎被破坏并且为空。
<input type="file" accept=".mp3" onchange="autoplay()">
<script>
var file, url, audio, source;
function autoplay(){
window.URL = window.URL || window.webkitURL;
file = document.querySelector("input[type=file]").files[0];
url = decodeURIComponent(window.URL.createObjectURL(file));
audio = document.createElement("audio");
source = document.createElement("source");
source.src = url;
audio.appendChild(source);
document.body.appendChild(audio);
audio.play();
}
</script>
编辑:
我在 FileReader 中使用了旧版本,也许这不是一个好的选择,但它可以工作......
解决方案
不要使用decodeURIComponent
只是做URL.createObjectURL
window.URL = window.URL || window.webkitURL;
function autoplay(evt) {
var file = document.querySelector("input[type=file]").files[0];
var url = URL.createObjectURL(file);
var audio = new Audio();
audio.src = url;
audio.play().catch(function(err){
console.error(err) // could not play audio
});
audio.controls = true;
document.body.appendChild(audio);
}
document.querySelector("input[type=file]").onchange = autoplay
<input type="file" accept=".mp3">
顺便说一句,我直接播放时遇到问题,这很奇怪,因为这event.isTrusted
是真的,这就是我将音频附加到 DOM 的原因
推荐阅读
- java - Java play框架为所有请求添加响应头
- css - 将图标放在它旁边和中间
- javascript - 使用 React Hooks 计算状态值的总和
- python - 在 python 中将二维数组写入 CSV 文件
- c++ - 使用 Boost C++ 使用管道将输出从子进程发送到父进程(将标准输出重定向到管道)
- firebase - Firestore 规则:用户访问包含 userId 的集合
- python - Python - 使用 PIL 从图像中提取 EXIF 元数据。无法复制 exifdata.com 产生的结果
- python - “kivy 中的 id 用法:” AttributeError:'super' 对象没有属性 '__getattr__'
- php - 在 php 中读取 csv 文件时不知道如何解决我的错误编码?
- kubernetes - helm 升级后 kubernetes 文件系统大小未更新