2012-10-10 14 views
5

Sto creando dinamicamente un file audio e cambiando la sorgente al volo. Tuttavia, dopo aver cambiato il src e provare a cambiare il tempo corrente, ottengo sempre un errore di stato non valido. Come si fa a testare per questo? O meglio attivare un evento quando è pronto e quindi chiamare currentTime per cambiare la sua posizione audio.audio HTML5 - test per errore di stato non valido (o Dom eccezione 11)

this.doneLoading = function(aTime){ 

    try{ 
     this.mAudioPlayer.currentTime = aTime/1000.0; 
    }catch(err){ 
     console.log(err); 
    } 
    this.mAudioPlayer.play(); 
} 

this.playAtTime = function(aTime) { 
    Debug("play at time audio: " + aTime); 
    Debug("this.mAudioPlayer.currentTime: " + this.mAudioPlayer.currentTime); 

    this.startTime = aTime; 

    if (this.mAudioPlayer.src != this.mAudioSrc) { 
     this.mAudioPlayer = new Audio(); 
     this.mAudioPlayer.src = this.mAudioSrc; 
     this.mAudioPlayer.load(); 
     this.mAudioPlayer.play(); 
     this.mAudioPlayer.addEventListener('canplaythrough', this.doneLoading(aTime), false); 
    } 
    else if ((isChrome() || isMobileSafari()) && aTime == 0) { 
     this.mAudioPlayer.load(); 
     this.mAudioPlayer.currentTime = aTime/1000.0; 
     this.mAudioPlayer.play(); 
     Debug("Reloading audio"); 
    }else{ 

     this.mAudioPlayer.currentTime = aTime/1000.0; 
     this.mAudioPlayer.play(); 
    }  



}; 

risposta

17

Per chi arriva dopo che effettivamente bisogno di un test di per evitare che questo errore stato non valido, si può provare questo:

if(this.readyState > 0) 
    this.currentTime = aTime; 

sembra funzionare per me comunque :)

+1

QUESTO FUNZIONA Penso che questa dovrebbe essere la risposta accettata THX – Prozi

9

Non stanno passando una funzione di riferimento al vostro addEventListener - si sta chiamando la funzione inline. La funzione doneLoading() esegue immediatamente (prima che il file è stato caricato) e il browser getta correttamente un INVALID_STATE_ERR:

this.mAudioPlayer.addEventListener('canplaythrough', this.doneLoading(aTime), false);

Prova a passare in una funzione di riferimento invece. Come questo:

this.mAudioPlayer.addEventListener('loadedmetadata',function(){ 
    this.currentTime = aTime/1000.0; 
}, false); 
+0

Grazie, questo è sicuramente un errore molto brutto da trascurare. – Neablis

Problemi correlati