【问题标题】:HTML5 audio tag doesn't play some audioHTML5 音频标签不播放某些音频
【发布时间】:2017-04-12 07:06:23
【问题描述】:

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

其中data[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个表格解决了,没有解决。

知道为什么会这样吗?

【问题讨论】:

    标签: javascript audio ipc electron webtorrent


    【解决方案1】:

    更新

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

    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 时,必须在.play() 方法之前调用.load() 方法。更改在下面的代码段中应用和评论。当然没有针对您的情况进行测试。

    片段

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

    【讨论】:

    • 不起作用。应用您的更改后我仍然有相同的结果,并且在加载请求后我有一个 console.log 消息,它没有出现在控制台中。我希望这对更多可能的回应有所帮助。 @zer00one
    • 嗨@rafaelleru,您使用音频标签或对象(new Audio)吗? toPlay 是自定义事件?
    • 音频标签@zer00ne
    • 我没有注意到自定义事件的代码:toPlay,贴出来了吗?
    • 是ipc消息标识符@zer00ne
    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2012-07-27
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多