Using JavaScript to change the playback rate of Audio and Video

Jun 25, 2021

I'm using the following code to provide variable playback speed in the course.

var player = GetPlayer();

var x = document.getElementsByTagName('video')[0];

x.playbackRate = 2.0;

 

The code only works once. If I have 2 videos, each on one slide. The code only works for the first slide video and not for the second slide.

If I'm exporting them as a single slide then it works for all. 

Any suggestions?

2 Replies
Math Notermans

Probably that is scope related. If you add a trigger on a slide...its related to that slide and only will work on that slide...

Your code will only work for 1 video. Because of the [0] behind it. That will get the first video on the slide.

If you code it like this...

var allVideos = document.getElementsByTagName("video");

it will get all videos.

However then you have to loop the collection of elements you found like this...

for (var i = 0; i < allVideos.length; i++) {
   allVideos[i].playbackRate = 2.0;
}

Kind regards,
Math

Saad Siddiqui

Hello Math,

Thank you so very much! It is a great help. 

I was putting this code on each individual slide with only 1 video on each slide. 

Try to provide ID to the element also but it was still working for the first slide, not the others.

Your looping works. Thank you again!

Kind Regards,

Saad Siddiqui