bbc / VideoContext

An experimental HTML5 & WebGL video composition and rendering API.
http://bbc.github.io/VideoContext/
Apache License 2.0
1.32k stars 156 forks source link

Bugfix: Preload audio playing #167

Closed richski closed 5 years ago

richski commented 5 years ago

This fixes (when released) https://github.com/bbc/VideoContext/issues/86, where the video/audio node plays audio when preloading.

You can see/hear the issue here - https://codesandbox.io/embed/priceless-moser-8cyys

As mentioned, this appears to be caused by the removal of the pause immediately after the VideoElementCache plays the video element.

Reverting this reintroduced https://github.com/bbc/VideoContext/pull/54, so the solution was to only pause the element if its associated video/audio node is not in the playing state.

More Details

A VideoElementCacheItem class has been created so a relationship ("link") between a cached video element and the MediaNode it is currently playing can be established.

Now, the VideoElementCache can check if the video should remain playing before attempting to pause it, getting around https://github.com/bbc/VideoContext/pull/54.

Potential issues

There was one unexpected consequences to this fix, where the sourceOffset calculation changes:

Previously:

If the sourceOffset = 10 and the start = 5, e.g.

var videoNode = vc.video('./media.mp4', 10, 5);
    videoNode.connect(vc.destination);
    videoNode.start(5);
    videoNode.stop(15);

The video will be visible at 5 seconds on the VideoContext timeline, but the actual media's currentTime will be 15 seconds, so the offset would be start + sourceOffset.

Now:

Using the same setup as above, the video will be visible at 5 seconds on the VideoContext timeline and the actual media's currentTime will be 10 seconds, so equal to the sourceOffset value.

This change seems to be more what I'd expect, but can anyone confirm this is OK?