Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  36] [ 6]  / answers: 1 / hits: 50828  / 11 Years ago, wed, november 27, 2013, 12:00:00

I have a video in the middle of my html. As you can see at first I haven't any source



<video id=videoPrincipal src= width=640 height=360 controls preload></video>


When I click a button I trigger a function that makes:



myVid = document.getElementById('videoPrincipal');
myVid.src = 'video.mp4';
myVid.play();
myVid.currentTime ='5';


The video starts playing correctly but I cant set the current time (and I made the same as we can see in http://www.w3schools.com/tags/av_prop_currenttime.asp)



It gives me the same error if I set the currentTime before calling the play function.



The error that shows me in the console is the following: Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. (in the currentTime line) but when i search this problem I cant associate with video, only with canvas.



Thanks in advance


More From » html

 Answers
11

You don't have to wait for it to start playing, but it does have to be ready to play. There's the canplay event to do that, so something like this should work:



myVid.play();
myVid.addEventListener('canplay', function() {
this.currentTime = 5;
});

[#74037] Tuesday, November 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruzs

Total Points: 710
Total Questions: 113
Total Answers: 100

Location: Nepal
Member since Sat, Jul 18, 2020
4 Years ago
cruzs questions
Thu, Nov 26, 20, 00:00, 4 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
Sun, Aug 2, 20, 00:00, 4 Years ago
;