Evento Audio Web "ENDED" Non viene licenziato durante la riproduzione di una parte di un buffer sorgente

StackOverflow https://stackoverflow.com//questions/25077703

  •  02-01-2020
  •  | 
  •  

Domanda

Abbiamo una funzione Audio Helper Web che riproduce suoni da un foglio audio e ci fa sapere quando sono finiti. In passato, abbiamo usato playbackState su un loop di aggiornamento per verificare un nodo che ha terminato la sua riproduzione, ma poiché Chrome 36 ora utilizza la specifica corrente, che non supporta playbackState abbiamo aggiornato il codice per utilizzare l'evento 'ended'.

Questo non sembra essere licenziato, comunque.

Quanto segue è la nostra funzione Playaudio: -

playAudio: function(trackName){
    var node = this._atx.createBufferSource();
    node.buffer = this._buffer;

    var gain = this._atx.createGain();

    node.connect(gain);
    gain.connect(this._atx.destination);

    var start = this._tracks[trackName][0];
    var length = this._tracks[trackName][1] - start;

    node.start(0, start, length);

    var sound = {name: trackName, srcNode: node, gainNode: gain, startTime: this._atx.currentTime, duration: length, loop: false, playHeadStart: start};

    if (this._features.hasEvents) {
        //node.addEventListener("ended", function () {
        //   this._soundEnded(sound)
        //}.bind(this));
        node.onended = function(){
            this._soundEnded(sound);
        }.bind(this);
    }

    this._playingSounds[++this._soundId] = sound;
    return this._soundId;
},
_soundEnded: function (sound) {
    this._playingSounds[sound] = null;
    delete this._playingSounds[sound];
}
.

La variabile this._features.hasEvents viene impostata impostando per node.onended !== null su un nodo audio creato durante il carico. Il buffer è creato da xhr'ing un file. Questo può essere un MP3 o un M4A, a seconda di ciò che stiamo giocando.

Qual è il motivo per cui la funzione _soundedEnded non viene chiamata? Ho provato sia node.onended = function(){...} e node.addEventListener("ended", function(){..}) Syntaxes, senza fortuna da nemmeno. Sembra che abbiamo lo stesso comportamento sia in IOS 7 Safari, sia al desktop / cromo Android.

È stato utile?

Soluzione

Ho appena risolto questo, dopo aver capito che stavo facendo qualcosa di stupido.L'errore non è con Webaudio, ma invece con la funzione di richiamata che dovrebbe rimuovere l'oggetto audio dall'array _playingSounds.

Questo bit del codice:

var sound = {name: trackName, srcNode: node, gainNode: gain, startTime:     this._atx.currentTime, duration: length, loop: false, playHeadStart: start};

if (this._features.hasEvents) {
    //node.addEventListener("ended", function () {
    //   this._soundEnded(sound)
    //}.bind(this));
    node.onended = function(){
        this._soundEnded(sound);
    }.bind(this);
}

this._playingSounds[++this._soundId] = sound;
return this._soundId;
.

avrebbe dovuto essere più simile a questo:

var sound = {name: trackName, srcNode: node, gainNode: gain, startTime: this._atx.currentTime, duration: length, loop: false, playHeadStart: start};
var soundId = ++this._soundId;
if (this._features.hasEvents) {

    node.onended = function(){
        this._soundEnded(soundId);
    }.bind(this);
}

this._playingSounds[soundId] = sound;
return soundId;
.

Stavo tentando di rimuovere l'oggetto con il valore, piuttosto che la chiave, che ovviamente non funziona.Il codice ora passa la chiave fino alla funzione e tutto funziona di nuovo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top