【问题标题】:Javascript Audio Play() function not working properly in Microsoft EdgeJavascript Audio Play() 函数在 Microsoft Edge 中无法正常工作
【发布时间】:2020-08-10 16:09:29
【问题描述】:

我正在尝试在 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 上运行良好,但我仍然不知道为什么在我的应用程序中仍然无法稳定运行。

【问题讨论】:

  • 您使用的是哪个版本的 Microsoft Edge?我已经测试了我这边的Audio play() sample(使用 Microsoft Edge Chromium(版本 81.0.416.64(官方版本)(64 位))和 Microsoft Edge 44.18362.449.0),它们都运行良好。请检查一下。如果还是不行,请尝试使用F12开发者工具检查是否有错误?并尝试清除浏览器数据并重新检查。此外,最好发布足够的代码或创建示例来重现问题。
  • 我的版本是“Microsoft Edge 44.18362.449.0, Microsoft EdgeHTML 18.18363”,如果您尝试使用单个文件,它会起作用,当您播放声音片段序列时会出现问题。前五个可能播放成功,但之后没有播放,控制台中也没有显示代码错误。
  • 这是我写的示例代码:let audioTrack = new Audio(file); audioTrack.preload = '自动';控制台.log(audio file [${file}] length: ${audioTrack.duration} sec.); audioTrack.onloadeddata = function () { console.log(audio: ${file} has successfully loaded.); }; audioTrack.load();

标签: javascript microsoft-edge html5-audio


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试删除console.log命令或修改代码如下:

    <script>
        function play() {
            var file = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-15.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>
    <button onclick="play()">Play Audio</button>
    

    以上代码在我这边运行良好(使用 IE、旧版 Microsoft Edge(Microsoft Edge 44.18362.449.0)和 Edge chromium(版本 81.0.416.64(官方构建)(64 位))浏览器)

    编辑

    要使用 JavaScript 顺序播放多个声音文件,您可以参考以下示例:

    样本 1:

    <script>
        var sounds = new Array(new Audio("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.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"),
            new Audio("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.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"),
            new Audio("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.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].addEventListener('ended', playSnd);
            sounds[i].play();
        };
    </script>
    

    sample 2(点击音频中的播放菜单,会依次播放多个声音):

    <audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
        <source type="audio/mp3" src="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3">
        Sorry, your browser does not support HTML5 audio.
    </audio>
    <ul id="playlist">
        <li class="active"><a href="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3">SoundHelix-Song-1.mp3</a></li>
        <li><a href="http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-128k.mp3">SoundHelix-Song-2.mp3</a></li>
        <li><a href="http://techslides.com/demos/samples/sample.mp3">SoundHelix-Song-3.mp3</a></li>
        <li><a href="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3">SoundHelix-Song-4.mp3</a></li>
        <li><a href="http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-128k.mp3">SoundHelix-Song-5.mp3</a></li>
        <li><a href="http://techslides.com/demos/samples/sample.mp3">SoundHelix-Song-6.mp3</a></li>
        <li><a href="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3">SoundHelix-Song-7.mp3</a></li>
        <li><a href="http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-128k.mp3">SoundHelix-Song-c8.mp3</a></li>
        <li><a href="http://techslides.com/demos/samples/sample.mp3">SoundHelix-Song-9.mp3</a></li>
    </ul>
    
    <script>
        var audio;
        var playlist;
        var tracks;
        var current;
    
        init();
        function init() {
            current = 0;
            audio = $('audio');
            playlist = $('#playlist');
            tracks = playlist.find('li a');
            len = tracks.length - 1;
            audio[0].volume = 0.6;
            playlist.find('a').click(function (e) {
                e.preventDefault();
                link = $(this);
                current = link.parent().index();
                run(link, audio[0]);
            });
            audio[0].addEventListener('ended', function (e) {
                current++;
                if (current == len) {
                    current = 0;
                    link = playlist.find('a')[0];
                } else {
                    link = playlist.find('a')[current];
                }
                run($(link), audio[0]);
            });
        }
        function run(link, player) {
            player.src = link.attr('href');
            par = link.parent();
            par.addClass('active').siblings().removeClass('active');
            audio[0].load();
            audio[0].play();
        }
    </script>
    

    【讨论】:

    • 再一次,如果你只玩一次,或者一个文件不会有问题。尝试将 50 个 mp3 文件依次播放。另外,当它不支持音频 API 时,您是如何在 IE 中运行此代码的? developer.mozilla.org/en-US/docs/Web/API/…
    • Audio objectplay() method都支持IE 11浏览器,请检查浏览器兼容性。另外,要使用 JavaScript 顺序播放多个声音文件,请参考上述示例。
    • 感谢回复,示例一更接近我的需要,如果您为每个剪辑创建一个新对象,我需要使用“new Audio()”构造函数而不是
    • 如果使用不同的文件,在Edge浏览器中仍然可以正常使用,请查看this screenshot(好像已经找到了)。关于您的应用程序无法以稳定的方式运行。我建议你可以尝试在这部分代码中设置调试器,并逐步调试它。您还可以检查是否可以获得正确的音频列表,并使用 F12 开发人员工具检查音频文件是否加载成功。此外,如果还没有找到真正的原因,我建议您可以使用示例解决方法。
    猜你喜欢
    • 2016-05-24
    • 2018-02-18
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多