以blob为源播放音频时出现DOMException

时间:2018-12-08 04:29:31

标签: javascript html

每当我运行此代码

clutax@clutax:/home/clutax > sudo lsb_release -a
[sudo] password for clutax: 
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:        17.3
Codename:       rosa


clutax@clutax:/home/clutax > sudo aptitude install postgresql-11
    The following NEW packages will be installed:
      libpq5{ab} postgresql-11{b} postgresql-client-11{a} 
    The following packages are RECOMMENDED but will NOT be installed:
      sysstat 
    0 packages upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
    Need to get 15,4 MB of archives. After unpacking 51,8 MB will be used.
    The following packages have unmet dependencies:
     postgresql-11 : Depends: libicu55 (>= 55.1-1~) which is a virtual package.
                     Depends: libllvm6.0 (>= 1:6.0~svn298832-1~) which is a virtual package.
                     Depends: libssl1.0.0 (>= 1.0.2~beta3) but 1.0.1f-1ubuntu2.27 is installed.
                     Depends: libstdc++6 (>= 5.2) but 4.8.4-2ubuntu1~14.04.4 is installed.
                     Depends: libsystemd0 which is a virtual package.
     libpq5 : Depends: libssl1.0.0 (>= 1.0.2~beta3) but 1.0.1f-1ubuntu2.27 is installed.
    The following actions will resolve these dependencies:

         Keep the following packages at their current version:
    1)     libpq5 [Not Installed]                             
    2)     postgresql-11 [Not Installed]                      
    3)     postgresql-client-11 [Not Installed]               


    Accept this solution? [Y/n/q/?] Y
    No packages will be installed, upgraded, or removed.
    0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    Need to get 0 B of archives. After unpacking 0 B will be used.

    clutax@clutax:/home/clutax > ^C

出现以下错误

var blob = new Blob(["ninja.mp3"], {type:"audio/mp3"});
var audio = new Audio(URL.createObjectURL(blob));
audio.play().catch(err => console.log(err));

我希望它播放音频文件DOMException index.html:3 ,但我遇到了此错误。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

完成时

var blob = new Blob(["ninja.mp3"], {type:"audio/mp3"});

您刚刚创建的文件是浏览器内存中的一个二进制文件,其中包含USVString ninja.mp3,并且在某些网络操作中,浏览器将为此文件发送一个Content-Type: audio/mp3标头。

理想,您刚刚创建了UTF-8文本文件。是的,MediaElement无法读取它。

var blob = new Blob(["ninja.mp3"], {type:"audio/mp3"});
// read as text
new Response(blob).text().then(console.log);

为进行比较,这是当以文本形式读取时真正的mp3文件的样子:

fetch("https://dl.dropboxusercontent.com/s/agepbh2agnduknz/camera.mp3")
 .then(resp => resp.text())
 .then(console.log)

Blob构造函数不希望使用URL,而是使用Blob部件列表(USVString,Blob或ArrayBuffers),但是绝不会获取任何内容

所以您想要的东西似乎很简单

var audio = new Audio("ninja.mp3");
audio.play().catch(console.log);

但是如果有一天您需要构建一个Blob(现在不需要),那么请确保在Blob()构造函数中传递的实际上是文件的二进制 content

答案 1 :(得分:0)

DOMException接口表示异常事件(称为异常),该异常事件是由于调用方法或访问Web API的属性而发生的。基本上,这就是在Web API中描述错误条件的方式。

我认为您错误地调用了该方法。请检查一下。