javascript - 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 上运行良好,但我仍然不知道为什么在我的应用程序中仍然无法以稳定的方式运行。
解决方案
谢谢大家,我发现了这个问题。我在调用 Audio.play() 之前使用了引导模式,由于某种原因,这会导致声音无法在 Microsoft Edge 中播放。所以我改变了顺序,我调用了模态函数,然后在“显示”事件中我调用了 Audio.play() 方法,这使它工作(有点稳定)。
推荐阅读
- keycloak - Keycloak URL 片段在登录时不会消失
- ruby-on-rails - 带有 Systemd 的 Puma 无法启动
- python - 使用 tf.data.map 将音频文件拆分为 1 秒的音频张量块?
- android - Android Detekt 构建失败
- python - 在循环中调用函数在 Python 中不起作用
- machine-learning - 为什么在 MNIST 分类器代码中使用 X[0] 会给我一个错误?
- ruby-on-rails - Ruby on Rails 5.2 - NoMethodError(nil:NilClass 的未定义方法“主机”):
- php - 使用按钮删除一行
- matrix - Power BI 矩阵 - 使用 DAX 减去两个(动态)对应列
- android-5.1.1-lollipop - 在 Delphi 10.4 上为 Android 5.1 开发?