2

我正在用电子构建一个音乐播放器。在主进程中,我将所有事件都包含在内容管理中,并通过 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种形式解决,无解。

知道为什么会这样吗?

4

1 回答 1

0

更新

我根据您的错误发现了一些东西:

Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()

这是和之间的竞争条件play()pause()

尝试:

var waitTime = 150;

setTimeout(function () {      
  if (el.paused) {
    el.play();
  }
}, waitTime);


老的

更改src音频或视频标签时,您必须在.load()方法之前调用.play()方法。更改在下面的代码段中应用和评论。当然,没有针对您的情况进行测试。

片段

ipc.on('toPlay', (event, data) => {
  console.log('http://localhost:9999/' + data[0].toString());

  audio_tag.src = 'http://localhost:9999/' + data[0].toString();
  play = true;

  //  .load() needs to be right before .play()
  /*~~~~~~~~~~~~~~~~~~~~~~~*/
  audio_tag.load();
  //~~~~~~~~~~~~~~~~~~~~~~~*/
  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]]);
  }
});

于 2016-11-28T19:38:17.103 回答