我使用Electron-forge框架并尝试制作音频播放器。当我从npm start
启动我的应用程序时,它运行良好。但是,当我使用npm run package
将应用程序编译为x64应用程序并从其.exe文件启动时,该应用程序无法正常工作。窗口创建,但音频不播放。
console.log中的错误:“未捕获(按承诺)DOMException:由于找不到受支持的源而无法加载。”
音频文件的路径绝对正确,我也将其编写为控制台。
编辑:我发现最好将Promise用作标签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();
}