2012-10-10 22 views
5

Estoy creando dinámicamente un archivo de audio y cambiando la fuente sobre la marcha. Sin embargo, después de cambiar el src y tratar de cambiar el tiempo actual, siempre recibo un error de estado Inválido. ¿Cómo haces para probarlo? O mejor enciende un evento cuando está listo y luego llama a currentTime para cambiar su posición de audio.Prueba de audio HTML5 para Error de estado no válido (o Excepción de Dom 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(); 
    }  



}; 

Respuesta

17

Para los que vienen después de que en realidad necesita una pruebapara evitar este error estado no válido, puede intentar esto:

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

parece funcionar para mí de todos modos :)

+1

ESTA OBRAS Creo que esta debería ser la respuesta aceptada THX – Prozi

9

No están pasando una función referencia a su addEventListener - que está llamando la función en línea. La función dedoneLoading() se ejecuta inmediatamente (antes de que el archivo se haya cargado) y el navegador lanza correctamente un INVALID_STATE_ERR:

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

Trate de pasar de una función de referencia lugar. De esta manera:

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

Gracias, Este es definitivamente un error muy malo para mí pasar por alto. – Neablis

Cuestiones relacionadas