首页 > 解决方案 > Javascript Audio Play() 函数在 Microsoft Edge 中无法正常工作

问题描述

我正在尝试在 Microsoft Edge 中调用 Javascript Audio Play() 函数,但它无法正常工作,有时可以正常工作,有时不能正常工作。我在 Chrome 和 Firefox 中尝试了相同的代码,并且效果很好。只有 MS Edge 不稳定。

我使用小于 300 KB 剪辑的 mp3 文件。

 let audioTrack = new Audio(file); 
 audioTrack.preload = 'auto'; 
 console.log(audio file [${file}] length: ${audioTrack.duration} sec.);
 audioTrack.onloadeddata = function () { 
   console.log(audio: ${file} has successfully loaded.); 
}; 
audioTrack.play();

更新:我更新了代码中的曲目列表:

<script>
    var sounds = new Array(new Audio("http://www.orangefreesounds.com/wp-content/uploads/2020/04/Heavy-synth-loop-126-bpm.mp3?_=1"),
        new Audio("http://www.orangefreesounds.com/wp-content/uploads/2020/04/Groove-synth-loop-130-bpm.mp3?_=1"),
        new Audio("http://www.orangefreesounds.com/wp-content/uploads/2020/04/Mystical-loop-118-bpm.mp3?_=1"),
        new Audio("http://www.orangefreesounds.com/wp-content/uploads/2020/04/New-generation-synth-loop-120.mp3?_=1"),
        new Audio("http://www.orangefreesounds.com/wp-content/uploads/2020/03/Synth-arp-music-loop-118.mp3?_=1"),
        new Audio("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3"),
       new Audio("http://soundbible.com/grab.php?id=2079&type=mp3"),
        new Audio("http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-128k.mp3"),
        new Audio("http://techslides.com/demos/samples/sample.mp3")
    );
    var i = -1;
    playSnd();

    function playSnd() {
        i++;
        if (i == sounds.length) return;
        sounds[i].onended = playSnd;
        sounds[i].play();
    };
</script>

它在 Edge 上运行良好,但我仍然不知道为什么在我的应用程序中仍然无法以稳定的方式运行。

标签: javascriptmicrosoft-edgehtml5-audio

解决方案


谢谢大家,我发现了这个问题。我在调用 Audio.play() 之前使用了引导模式,由于某种原因,这会导致声音无法在 Microsoft Edge 中播放。所以我改变了顺序,我调用了模态函数,然后在“显示”事件中我调用了 Audio.play() 方法,这使它工作(有点稳定)。


推荐阅读