Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari blocks play() on video despite being called from click event

I'm creating some custom video controls for an html5 element. I've bound a click event handler to a play/pause button which calls .play() on the corresponding video.

From my research, Safari will block calls to .play() unless you are in a click handler, however it is blocking my calls to .play() despite the fact that I am triggering it from within a click handler, like so:

$('.video-container .play-pause').click(function(event){
    var $video = $(event.currentTarget).parent().find('video');
    if($video[0].paused)
      $video[0].play();
    else
      $video[0].pause();
});

And the error:

Unhandled Promise Rejection: NotSupportedError (DOM Exception 9): The operation is not supported.

which is originating from $video[0].play();.

Safari Version 11.0.1 (13604.3.5)

OSX High Sierra 10.13.1 (17B48)

Any ideas?

like image 326
Max Mumford Avatar asked Nov 14 '17 12:11

Max Mumford


People also ask

How to block video on safari?

Block video for all websites 1 In the Safari app on your Mac, choose Safari > Preferences, then click Websites. 2 Click Auto-Play in the list on the left. 3 Do any of the following: Choose settings for a website in the list: Select the website on the right, then choose the option you want for it. ...

How to enable auto play on safari?

Block video for the currently displayed website In the Safari app on your Mac, choose Safari > Settings for This Website. Hold the pointer to the right of Auto-Play, then click the pop-up menu and choose an option: Allow All Auto-Play: Lets videos on this website play automatically.

Can you block auto-playing media on a specific domain on safari?

Safari, which Mac users should be using instead of Chrome anyway, now blocks almost all auto-playing videos with sound by default. You can go a step further and block all auto-playing media on any particular domain. But what if actually want certain sites to auto-play?

Why is my click event not working in Safari?

This works fine in Firefox, Chrome, IE, but in some cases the click event is not triggered on mobile Safari (iPad and iPhone). These elements all have exactly the same CSS, it's just their position that differs (they are in different containers). I tried various solutions that I found here but with no luck. For instance:


2 Answers

It's worth noting that this error occurs whenever you call .play() on a video that Safari has failed to load.

Something else interesting is that Safari requires the web server to support the byte-range requests to correctly load video files. See note here: https://stackoverflow.com/a/36299252

I recently had an issue with serving files from the php command line process instead of a dedicated web server like Apache or nginx and couldn't work out why videos were playing on the live site, but not in my development environment.

A quick way to determine this is to load the video directly from the address bar. If it fails to load, your server likely doesn't support byte-range requests.

like image 143
lachie_h Avatar answered Oct 25 '22 02:10

lachie_h


Eugh. The solution was to use an absolute path for the video source, not a relative one.

This is wrong: <video src="assets/vid.mp4"></video>

This is correct: <video src="http://example.com/assets/vid.mp4"></video>

like image 41
Max Mumford Avatar answered Oct 25 '22 01:10

Max Mumford