我正在用电子构建一个音乐播放器。在主进程中,我将所有事件都包含在内容管理中,并通过 http 服务器在主进程中创建在渲染器中加载音乐,因此audio
标签具有src
类似src = http://localhost:9999
. 歌曲是通过 webtorrent 从 torrent 下载的。我的问题是我改变了当前正在播放的种子,例如从下载的第一个种子到第二个种子,因为第一个种子没有更多内容可以播放,我用新内容启动新服务器并尝试播放音频没有启动但登录主进程的声音说音频已加载到 http 服务器上。此外,如果我手动单击第二个种子的其他歌曲,音频效果很好,但我在控制台上收到以下错误:
Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()
设置音频标签 src 的函数是:
ipc.on('toPlay', (event, data) => {
console.log('http://localhost:9999/' + data[0].toString());
audio_tag.src = 'http://localhost:9999/' + data[0].toString();
play = true;
audio_tag.play();
audio_tag.onended = function(){
console.log('play end, to play ' + (data[0]+1).toString() + 'from torrent number: ' + data[1].toString());
ipc.send('getPlayData', [data[0]+1, data[1]]);
}
})
wheredata[0]
表示 torrent 中文件的索引,因为完整的 url 是http://localhost:9999/<index of file>
主进程发送这样的ipc消息:
ipc.on('getPlayData', function(event, data) {
var torrent = data[1];
var file = data[0];
if((file <= downloaderInstance.getTorrentFiles(torrent).length) && (downloaderInstance.getTorrentFiles(torrent)[file].name.indexOf('mp3') != -1)){
var i = file;
while(downloaderInstance.getTorrentFiles(torrent)[i].name.indexOf('mp3') == -1 &&
file <= downloaderInstance.getTorrentFiles(torrent).length){
file++;
}
} else {
torrent++;
}
if(torrent <= downloaderInstance.getNTorrents() && torrent != data[1]){
file = 0;
while(downloaderInstance.getTorrentFiles(torrent)[file].name.indexOf('mp3') == -1 &&
file <= downloaderInstance.getTorrentFiles(torrent).length){
file++;
}
} else if (torrent >= downloaderInstance.getNTorrents() && torrent != data[1]){
torrent = 0;
file = 0;
while(downloaderInstance.getTorrentFiles(torrent)[file].name.indexOf('mp3') == -1 &&
file <= downloaderInstance.getTorrentFiles(torrent).length){
file++;
}
}
if(torrent != currentPlayingTorrent){
currentPlayingTorrent = torrent;
if(currentPlayingTorrent != undefined)
downloaderInstance.closeTorrentServer();
downloaderInstance.initTorrentServer(currentPlayingTorrent);
downloaderInstance.listen();
}
console.log(file + ' ' + torrent);
console.log('toplay ' + file.toString() + '(' + downloaderInstance.getTorrentFiles(torrent)[file].name + ')' + ' from ' + torrent.toString());
event.sender.send('toPlay', [file, torrent]);
})
错误只出现在更换torrent时,我尝试了100种形式解决,无解。
知道为什么会这样吗?