我使用 Electron-forge 框架并尝试制作音频播放器。当我从它启动我的应用程序时, npm start
它运行良好。但是,当我使用npm run package
x64 应用程序编译应用程序并从其 .exe 文件启动时,该应用程序无法正常工作。窗口创建,但音频不播放。
console.log 中的错误:“未捕获(承诺)DOMException:加载失败,因为找不到支持的源。”
音频文件的路径绝对正确,我也将它们写到控制台。
编辑:我发现最好将 Promise 用于 tag audio
,但问题仍然存在。代码:
$(document).ready(function () {
prepare_song('D:' + '\\' + 'Downloads' + '\\' + 'audio_1.mp3');
$("#button_play_pause").click(function () {
console.log("click play");
var playPromise = document.querySelector('audio').play();
if (playPromise !== undefined) {
playPromise.then(function () {
console.log("play !");
// triggered from npm start and music is playing
}).catch(function (error) {
console.log("play error:" + error);
// triggered from npm run package (x64 .exe app) Error: NotSupportedError: The element has no supported sources.
});
}
});
});
function prepare_song(filepath) {
console.log(" prepare: " + filepath);
$("#audio").attr("src", filepath);
let audio = document.getElementById('audio');
audio.load();
}