How to change currentTime of an HTML created audio element?

2

I have seen different examples of

elemento.currentTime

But I have not seen any with an element created from javascript to which it is applied and the following code does not work.

var time;
var audioe = document.createElement("audio");
audioe.src="http://soundbible.com/mp3/Tiger Growling-SoundBible.com-258880045.mp3";
audioe.onloadeddata = function() {
audioe.currentTime = 2;
audioe.play();
};

Someone sees the problem: /?

    
asked by Ismael 22.02.2016 в 20:08
source

1 answer

1

No problem at all, even in your example, currentTime is working as it should.

What fails in the code above: you use an audio that lasts 2,063 seconds, then when you start it in the second 2.0, there really is nothing left to listen to. If instead of going to the second 2, you go to the 1.1, you will see (or hear) as currentTime works correctly:

var time;
var audioe = document.createElement("audio");
audioe.src="http://soundbible.com/mp3/Tiger Growling-SoundBible.com-258880045.mp3";
audioe.onloadeddata = function() {
  audioe.play();
  audioe.currentTime = 1.1;
};
audioe.onended = function(e) {
  console.log("El audio dura: " + audioe.currentTime);
};
document.querySelector("body").appendChild(audioe);
    
answered by 22.02.2016 в 23:24