Home › Forums › JavaScript › Media Events Not Working in Safari/Chrome
- This topic is empty.
-
AuthorPosts
-
August 8, 2013 at 7:09 pm #146191
realph
ParticipantI’ve got an alert that pops up when the video on said page plays and finishes. Any reason why this is no longer working in Safari/Chrome? It was working as recently as about 2 months ago.
$(document).ready( function() { var v = document.getElementsByTagName("video")[0]; v.addEventListener("playing", function() { alert("Video has started"); }); v.addEventListener("ended", function() { alert("Video has ended"); }); });
Still works perfectly fine in Firefox.
Any help is appreciated. Thanks in advance.
August 8, 2013 at 8:33 pm #146199g3logic
ParticipantLooks like you’re using jQuery. Try using bind() to handle your video events. I have found that adding event listeners in JS for audio/video events can be wonky sometimes.
Try this (note – omit the [0] in the video reference when working with video events):
var v = $('#video'); v.bind('playing', function() { alert("Video has started"); }); v.bind('ended', function() { alert("Video has ended"); });
August 8, 2013 at 8:49 pm #146202realph
Participant@g3logic So do I just replace my code with your code? Do I wrap it in the same .ready() method?
August 8, 2013 at 9:01 pm #146203g3logic
ParticipantYes, just replace the code that you posted with the code that I posted and you should be good to go. Just remember that you cannot refer to video events unless the video element exists. So this code assumes you have a video tag in your html with an id=”video”
Yes, place it inside of the $(document).ready( function() { });
August 9, 2013 at 6:18 am #146250realph
Participant@g3logic Again, that seem to be working just fine in Firefox. But no Safari or Chrome. Any idea what the problem may be?
August 9, 2013 at 8:47 am #146255g3logic
ParticipantAny way you could post to CodePen?
August 9, 2013 at 8:53 am #146256Paulie_D
MemberPop-ups disabled?
August 9, 2013 at 2:37 pm #146294realph
ParticipantIt seemed to be a problem with the HTML5 Player I was using (SublimeVideo) as I tried it on CodePen and it worked perfectly.
http://codepen.io/realph/pen/rfmdz
So, instead of using native
.addEventListener
‘s, I’m using Sublime’s API and have managed to get it working.sublime.ready(function(){ var player = sublime.player('the-video'); sublime.player('the-video').on({ start: function(player) { alert("Video has started") }, end: function(player) { alert("Video has ended"); } }); });
P.S. I probably should have mentioned I was using Sublime in the beginning.
Thanks for all the help @g3logic!
August 9, 2013 at 3:05 pm #146300g3logic
ParticipantNo problem. Glad you figured it out!
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.