简体   繁体   English

如何在WaveSurfer.js中检测自动播放何时失败?

[英]How to detect when Autoplay fails in WaveSurfer.js?

I am accustomed to using the javascript media promise to detect when autoplay has failed as outlined here: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes . 我习惯于使用javascript媒体承诺来检测何时自动播放失败,如下所示: https : //developers.google.com/web/updates/2017/09/autoplay-policy-changes But how do you detect an autoplay failure in WaveSurfer.js? 但是,如何在WaveSurfer.js中检测到自动播放失败?

I've tried the error event but it doesn't appear to do anything in this case. 我尝试了错误事件,但在这种情况下似乎没有任何作用。 What's even stranger is that the play event will fire regardless of whether it's actually playing or not. 甚至更奇怪的是,无论是否实际在播放,播放事件都会触发。

https://jsfiddle.net/mcrockett/k985zjh2/18/ https://jsfiddle.net/mcrockett/k985zjh2/18/

    var wavesurfer = WaveSurfer.create({
        container: '#waveform',
        waveColor: 'red',
        progressColor: 'purple'
    });

    function play() {
        wavesurfer.playPause();
    }

    wavesurfer.on('ready', play);

    wavesurfer.on('error', function(){
        console.log("does not get called when autoplay fails :(");
    });

    wavesurfer.on('play', function(){
        console.log("called even when autoplay fails");
    });




 wavesurfer.load('https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3');

See my jsfiddle for an example. 有关示例,请参见我的jsfiddle。 If viewed through Chrome, autoplay will usually fail. 如果通过Chrome浏览器查看,自动播放通常会失败。

After searching around, I found that WaveSurfer JS uses WebAudio (an object called AudioContext is created). 在四处搜寻之后,我发现WaveSurfer JS使用WebAudio(创建了一个名为AudioContext的对象)。 According to the Google page : 根据Google页面

Key Point: If an AudioContext is created prior to the document receiving a user gesture, it will be created in the "suspended" state, and you will need to call resume() after a user gesture is received. 关键点:如果在文档接收用户手势之前创建了AudioContext,它将以“已暂停”状态创建,并且在收到用户手势之后您需要调用resume()。

Then I realized, "hey, can't I add my own AudioContext to WaveSurfer??" 然后我意识到,“嘿,我不能将自己的AudioContext添加到WaveSurfer吗?” Yes, I can. 我可以。 One of the WaveSurfer options is 'audioContext.' WaveSurfer 选项之一是“ audioContext”。

Therefore, the idea is to create my own AudioContext, plop it into WaveSurfer, and read the state of the AudioContext to find out if it is suspended (auto-play failed) or not. 因此,这个想法是创建我自己的AudioContext,将其放入WaveSurfer中,并读取AudioContext的状态,以查明它是否被挂起(自动播放失败)。

I have never done anything with Web Audio. 我从未对Web Audio做任何事情。 But after digging around I came up with this: 但是在深入研究之后,我想到了:

// Fix up for prefixing
window.AudioContext = window.AudioContext||window.webkitAudioContext;
var context = new AudioContext();

if(context.state === 'suspended')
  alert("auto-play failed!!");

var wavesurfer = WaveSurfer.create({
  container: '#waveform',
  waveColor: 'red',
  progressColor: 'purple',
  audioContext: context
});

https://jsfiddle.net/mcrockett/02azn6Lb/6/ https://jsfiddle.net/mcrockett/02azn6Lb/6/

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

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