javascript - HTML5音频标签无法播放某些音频

标签 javascript audio ipc electron webtorrent

我正在用 Electron 打造音乐播放器。在主要流程中,我将所有事件都进行了内容管理,并且我通过主要在http服务器上创建的渲染器将音乐加载到渲染器中,因此audio标签具有src属性,例如src = http://localhost:9999。歌曲是使用torrent从torrent中下载的。我的问题是我将当前正在播放的种子更改为当前的种子,例如从下载的第一种子更改为第二种子,因为第一种子没有更多内容可以播放,因此我将新服务器初始化为新内容并尝试播放声音没有开始,但登录主过程时说音频已加载到http服务器上。另外,如果我手动单击此第二个torrent的其他歌曲,音频效果很好,但是在控制台上出现以下错误: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]]); 
    }
})

其中data[0]代表torrent中文件的索引,因为完整的网址是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种形式来解决它而没有解决方案。

知道为什么会这样吗?

最佳答案

更新

我发现了一些基于您的错误的信息:

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



这是 play() and pause() 之间的比赛条件

尝试:
var waitTime = 150;

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

老的

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

SNIPPET

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]]);
  }
});

关于javascript - HTML5音频标签无法播放某些音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40846348/

相关文章:

c# - 在C#记录设备上播放声音

vb.net - 缺少依赖项时如何避免异常? (WMP控制)

ipc - OPC 和 DDS 之间的区别?

c - 如何在 ListView 控件中查找项目?

linux - 你如何关闭一个Qt子进程并让子进程执行清理代码?

javascript - 如何在循环中使用ajax发送单个请求

javascript - 将选中的图像渲染到指定的图像容器

audio - 从CMD运行时会播放声音,但不能像Jar一样运行

javascript - 为什么 event.clientX 在 firefox 中对于 dragend 事件错误地显示为 0?

javascript - 使用类选择器附加事件处理程序是否会影响该类的动态添加元素?