繁体   English   中英

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

[英]HTML5 audio tag doesn't play some audio

我正在用电子打造音乐播放器。 在主要流程中,我将所有事件都进行了内容管理,并且我通过主要在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中文件的索引,因为完整的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种形式来解决它而没有解决方案。

知道为什么会这样吗?

UPDATE

我根据您的错误发现了一些问题:

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()方法。 更改将在下面的代码段中应用和评论。 当然没有针对您的情况进行测试。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM