Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测是否";MediaElementAudioSource由于对http的CORS访问限制而输出零…”;_Javascript_Html_Google Chrome_Firefox_Web Audio Api - Fatal编程技术网

Javascript 检测是否";MediaElementAudioSource由于对http的CORS访问限制而输出零…”;

Javascript 检测是否";MediaElementAudioSource由于对http的CORS访问限制而输出零…”;,javascript,html,google-chrome,firefox,web-audio-api,Javascript,Html,Google Chrome,Firefox,Web Audio Api,在使用Web音频API时,我在Firefox控制台中看到了这条消息 传递给createMediaElementSource的HTMLMediaElement具有跨源资源,节点将输出静默 和Chrome控制台中的一样 MediaElementAudioSource由于http的CORS访问限制而输出零 我明白了。我的问题是:如何从JavaScript代码的角度检测音频/视频html元素的此类限制?我确实希望在调用“createMediaElementSource”以防止音频静音之前捕获它。这对我来

在使用Web音频API时,我在Firefox控制台中看到了这条消息

传递给createMediaElementSource的HTMLMediaElement具有跨源资源,节点将输出静默

和Chrome控制台中的一样

MediaElementAudioSource由于http的CORS访问限制而输出零

我明白了。我的问题是:如何从JavaScript代码的角度检测音频/视频html元素的此类限制?我确实希望在调用“createMediaElementSource”以防止音频静音之前捕获它。

这对我来说很有用:

function _reallyPlayAudioURL(url)
{
    //...stuff
    // createMediaElementSource...
    //...etcetera
}
playAudioURL(url)
{
    if ( ! url) return;
    var xhr = new XMLHttpRequest();
    xhr.open("HEAD", url);
    xhr.onload = (_) => { _reallyPlayAudioURL(url); }
    xhr.onerror = (_) => { var msg
             = "Can't play audio for either or both of the following reasons:\n"
        msg += "  1) the file does not exist, it is damaged, unrecognized, etc.\n"
        msg += "  2) your browser is outputting silence pursuant to its CORS policy.\n\n";
        msg += "Please take the following precautions:\n"
        msg += "  1) ensure that audio file exists undamaged.\n"
        msg += "  2) restart your browser with the\n";
        msg += "       --allow-file-access-to-files\n";
        msg += "     switch on the command line.";
        alert(msg);
    }
    xhr.send();
}