javascript - 在 ts/js 和电子中使用 setInterval 播放音频
问题描述
我正在用电子创建一个节拍器,使用嚎叫器播放音频。
当窗口在屏幕上时,音频会按应有的方式播放,但是当我最小化窗口时,音频开始以错误的间隔播放并且节拍器出现故障。
这些是我用来播放节拍的音频
const mainBeat = new Howl({
src: ["sounds/Boutique808.mp3"],
onplayerror: (e) => console.log(e),
});
const secondBeat = new Howl({
src: ["sounds/tick.mp3"],
onplayerror: (e) => console.log(e),
});
startBeats 函数创建由函数 bpmToMs 计算的间隔,如果 bpm 为 60,则结果为 750ms
如果 currentBeat 为 1 或 0 则函数 playBeat 播放 mainBeat 否则播放第二个节拍
startBeats = (..._: any[]) => {
this.setState({ playing: true });
this.playBeat();
this.beatInterval = setInterval(() => {
this.playBeat();
}, bpmToMs(this.state.currentBpm));
};
playBeat = () => {
this.incrementBeat();
if (this.state.currentBeat === 1 || this.state.currentBeat === 0)
mainBeat.play();
else secondBeat.play();
};
我已经从我的 react 类组件中复制了必要的行,所以不用担心意外的单独语法:)
解决方案
我通过在电子 webPreferences 中添加backgroundThrottling: false解决了这个问题
mainWindow = new MainWindow({
width: width,
height: height,
minWidth: width,
minHeight: height - 300,
maxWidth: width,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
backgroundThrottling: false,
},
icon: path.join(__dirname, "assets/icons/png/1204x1024.png"),
});
现在它工作得很好!
推荐阅读
- excel - 为什么我的代码找不到正确的单元格并识别重复项?
- r - 如何增加 R Markdown 中项目符号点之间的距离?
- python - PySpark - 按数组列分组
- python - SAS 数据集用 ssh 连接描述
- mysql - 加入子查询外的列
- php - 如何使用 PHPMailer 发送带有主题/wordpress 框架的电子邮件?
- apache-tika - 有没有办法在 tika-server 中关闭嵌入式文档的解析?
- csv - 如何在 mule 4 中转置 csv?
- javascript - Reactjs 从日历中异步加载事件 - Google Calendar API
- c# - 如何以最易读的方式使用 LanguageExt 在 C# 中构建嵌套的 HashMap?