Pause a Vzaar Video in a Bootstrap Modal - javascript

Video in a Bootsrap modal continues to play when the modal is closed.
I have found solutions for YouTube video and html5 video but i am using Vzaar Video and Vzaar's API java script
I have it working from the close button in the modal (using id="video-btn") but I really need it to work also if clicking outside of the modal, or I guess on data-dismiss="modal", which should cover all (?).
Not being great with java, help would be appreciated.
My code just for the close button (which works fine) is below.
// Vzaar
window.addEventListener("load", function() {
var player = new vzPlayer("vzvd-XXXXXXX");
// adding pause() API method to DOM element:
var myPauseButton = document.getElementById("video-btn");
myPauseButton.addEventListener("click", function() {
player.pause();
});
});

I have solved it:
// add listener vzaar video //
window.addEventListener("load", function() {
var player = new vzPlayer("vzvd-xxxxxxx");
// adding pause() API method to hidden.bs.modal: //
$('#myModalId').on('hidden.bs.modal', function () {
player.pause();
});
});
(of course http://player.vzaar.com/libs/flashtakt/client.js needs to be included)

Related

using popups for divi and the diviarea functions, need to add code to my code snippet to pause a background video when a popup is triggered

what I need help with is adding some code to a snippet I have that autoruns a video in a popup when the popup is triggered and it auto closes the popup when the video ends. However, I have a couple of pages that also have a divi section background video. and I'm seeing some performance glitches. so I am aiming to add some code that pauses the background video when the popup is triggered.
I did find this snippet that pauses a background video on mouse hover so I think I am on the right track.
window.onload = function() {
let videoBg = document.getElementsByClassName('mejs-mediaelement')[0].firstElementChild.firstElementChild;
let bg = document.getElementsByClassName('et_pb_section_video')[0];
videoBg.pause();
bg.addEventListener('mouseover', function() {
videoBg.play();
});
bg.addEventListener('mouseleave', function() {
videoBg.pause();
});
}
so this tests out good on my pages the video pauses when the mouse leaves the page.
here is my code snippet I currently have to control auto play and auto close.
jQuery(document).ready(function($) {
homeVideoElement1 = $("#divi-video-container1 video")[0];
homeVideoElement2 = $("#divi-video-container2 video")[0];
DiviArea.addAction('show_area', function(area) {
if (area.hasId('sim-intro')) {
homeVideoElement1.play();
}
if (area.hasId('libraryintro')) {
homeVideoElement2.play();
}
// console.log('im open');
});
$('video').on('ended', function() {
//console.log('Video has ended!');
$('a.da-close').trigger('click');
});
});
could someone help me with some code to add to my snippet to pause the background video? is someone familiar with divimode.com js API?
at the moment I am unsure how to proceed, divimode.com's documentation has some broken links
so halfway down the links to JS API Overview and Code Generator return a 404 error. I did leave a message with the customer support sales side of things on divimode.com I don't have access to their tech support side.

Remove a DOM element when the app goes to the background

I am trying to remove an iframe from the DOM when the user leaves my Ionic app so the YouTube video stops playing.
I am using the pause and resume events:
$ionicPlatform.on('resume', function() {
$rootScope.$broadcast('onResume');
});
$ionicPlatform.on('pause', function() {
$rootScope.$broadcast('onPause');
});
The pause event is triggered when I leave the app and the resume event is triggered when I open it again (I checked the time to make sure they were not both triggered on resume) but the iframe is not removed from the DOM.
I use ng-if="showVideo" on the div wrapping the iframe and $scope.showVideo = false; to remove it:
$scope.$on('onPause', function () {
$scope.showVideo = false;
alert(Date.now() / 1000);
});
It works perfectly when I switch pages using the beforeLeave event:
$scope.$on('$ionicView.beforeLeave', function () {
$scope.showVideo = false;
});
Is there some kind of limitation on what Ionic can do when the app is in the background? Are there other ways to stop a video when the user leaves the app?
I solved it using the YouTube iframe API:
$scope.$on('onPause', function () {
$('iframe').each(function () {
this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')
});
});
It turns out that we can't modify the DOM when the app is in background mode but we can communicate with the iframe.

Simple pause with vimeo api Froogaloop

Trying to call the vimeo api to pause video on a click event to hide it. And, on clicking to reveal the video again, play the video from its paused position.
There are various related questions on here, I can't find an answer to the simple "how to pause". I'm a jquery novice and can't make heads or tails of the froogaloop documentation.
Here's a FIDDLE, and my current jquery script to hide the video
$(document).click(function (event) {
if (!$(event.target).hasClass('click')) {
$('#video').hide();
}
});
which hides it when an element without the "click" class is clicked. But the video plays on in the background. Froogaloop is loaded in the fiddle. Thanks everyone
Here's an updated FIDDLE that makes pause/play work as I'd imagined. Click the image to play the video; click outside or on empty space (you control this with classes) to pause it; click image again to play from paused position. Simple, no buttons, no excess jquery or froogaloop code.
Putting this here for those who might benefit from it. And a +1 to mbrrw for getting me started.
var iframe = $('#video iframe')[0];
var player = $f(iframe);
$('.showvideo').on('click', function(){
$('#video').show();
$('.image').hide();
player.api('play');
});
$(document).click(function (event) {
if (!$(event.target).hasClass('click')) { //if what was clicked does not have the class 'click' (ie. any empty space)
$('#video').hide();
$('.image').show();
player.api('pause');
}
});
Remember to append api=1 to the vimeo link. And the url must be https, not http.
froogaloop can be a pain in the arse.
The code to get you started is here:
https://developer.vimeo.com/player/js-api#universal-with-froogaloop
I've adapted that to get it working i think how you expect here:
https://jsfiddle.net/fLe5xs4v/
Setting it up like so:
var iframe = $('#video iframe')[0];
var player = $f(iframe);
Note that if you change the text in the play and pause buttons you will break this code:
$('button').bind('click', function() {
player.api($(this).text().toLowerCase());
});
Give it a shot it should get you going in the right direction at least. Good luck!

automatically play next soundcloud

I'm currently working on my music blog, and I'm trying to make the widgets start when previous one finishes. I have both soundcloud and youtube widgets, but the priority is to implement this functionality for the soundcloud ones.
Each iframe has 2 class .widget &.soundcloud_widget for soundcloud and .widget & .youtube_widget for youtube. Finally, each widget gets an id "widget1", "widget2", etc... in the order of apparition.
The idea would be to get the event "SC.Widget.Events.FINISH" , to get the id of the corresponding widget, "increment" it, and then make the next one play.
Unfortunately, I'm struggling with javascript & the documentation of soundcloud, and I would really appreciate some help !
Thanks in advance
PS : here is the code that does the above, I have nothing else that is usable...
<script type="text/javascript">
function autoPlayInitiate () {
var i=1;
$("iframe[src^='https://w.soundcloud.com/']").addClass("widget soundcloud_widget");
$("iframe[src^='//www.youtube.com']").addClass("widget youtube_widget");
$('.widget').each(function(){
$(this).attr('id','widget'+i);
i++;
});
}
window.onload = autoPlayInitiate;
$(document).ready(function () {
var ScWidget = $('.soundcloud_widget');
}
</script>
So first off I think the simplest way for you to solve this problem is to pick one of the players (either youtube or soundcloud) and use there playlist features to showcase your sounds.
here is a live example of using a playlist in Sound Cloud you can play with
http://runnable.com/UuhEs3Yq2_8hAACT/embbed-soundcloud-widget-using-javascript
relevant code:
function embed (id) {
SC.oEmbed("http://soundcloud.com/" + id // user or playlist to embed
, { color: "ff0066"
, auto_play: true
, maxwidth: 500
, maxheight: 1000
, show_comments: true } // options
, document.getElementById("scWidget") // what element to attach player to
);
}
$(document).ready(
function () {
embed("dj-faze");
}
);
however if you wish to go the long route you will want to familiarize yourself with the widget API:
http://developers.soundcloud.com/docs/api/html5-widget#
the bind(eventName, listener) is what you would be most interested in
var iframeElement = document.querySelector('iframe');
var iframeElementID = iframeElement.id;
var widget = SC.Widget(iframeElement);
widget.bind(SC.Widget.Events.FINISH, function () {
alert('add code to play next song here')
});
I will try to put up a new example on runnable once the site has finished its maintenance

Getting Current YouTube Video Time

I am writing a Browser Plugin and need to find a way to get the current time a YouTube Video playing on YouTube using JavaScript. I have been playing around in the Chrome JavaScript Console and haven't had any luck.
It appears that the chrome API only works with embedded video players not a video that is playing on on youtube.com. One option I looked into is in the share section of a video their is an input box for the "start at:" time that contains the current time of the video. I have tried using .value and .text on this input box and they both return undefined? Does anyone have any ideas?
ytplayer = document.getElementById("movie_player");
ytplayer.getCurrentTime();
See the api
Update: if it didn't work, also try player.playerInfo.currentTime (codepen live example)
Depends on what you want
player.getCurrentTime():Number
Returns the elapsed time in seconds since the video started playing.
player.getDuration():Number
Returns the duration in seconds of the currently playing video. Note
that getDuration() will return 0 until the video's metadata is loaded,
which normally happens just after the video starts playing.
http://code.google.com/apis/youtube/js_api_reference.html
Finally I found how to make it work on iOS (and Android too).
Actually, the whole youtube js api was broken for me if run on mobile browser.
Problem solved by creating player using new YT.Player as described in YouTube IFrame API.
Please note: only creating <iframe> from <div> placeholder works for mobile browsers at the time. If you try to use existing <iframe> in new YT.Player call, as mentioned in IFrame API, this will not work.
After player created, it's possible to use player.getCurrentTime() or player.getDuration() with player instance created.
Note: I had no luck calling this methods on player obtained with
player = document.getElementById(...) (from #JosephMarikle answer).
Only created player instance worked in mobile browsers.
Useful links:
YouTube IFrame API
YouTube JavaScript API
YouTube Player Demo
You can use Html5 Video API on youtube.com
var htmlVideoPlayer = document.getElementsByTagName('video')[0];
htmlVideoPlayer.currentTime
Note: It's not gonna work on Youtube Iframe API because Iframes are isolated. You cannot access the context of a Youtube IFrame .
In 2020, this works:
player.playerInfo.currentTime
full code:
see it live on codepen
Just FYI, There is a new iframe API for the YouTube player:
https://developers.google.com/youtube/iframe_api_reference
document.querySelector('video').currentTime
Stop at specific time youtube video and show notification box in GWD
<script>
var yid = document.getElementById("gwd-youtube_1");
var idBox = document.getElementById("box1");
pausing_function = function(event) {
var aa = setInterval(function() {
if (yid.getCurrentTime() > 8.0 && yid.getCurrentTime() < 8.1) {
yid.pause(yid);
idBox.style.opacity = 1;
console.log(yid.getCurrentTime() + "playing")
clearInterval(aa);
yid.removeEventListener("playing", pausing_function);
}
}, 100)
}
yid.addEventListener("playing", pausing_function);
var pausing_function_1 = function() {
if (yid.getCurrentTime() > 8.1) {
console.log(yid.getCurrentTime() + "pause")
// remove the event listener after you paused the playback
yid.removeEventListener("playing", pausing_function);
}
};
</script>
play video and hide notification
<script type="text/javascript" gwd-events="handlers">
window.gwd = window.gwd || {};
gwd.pauseVideo = function(event) {
var idBox = document.getElementById("box1");
idBox.style.opacity = 0;
};
</script>
<script type="text/javascript" gwd-events="registration">
// Support code for event handling in Google Web Designer
// This script block is auto-generated. Please do not edit!
gwd.actions.events.registerEventHandlers = function(event) {
gwd.actions.events.addHandler('gwd-youtube_1', 'playing', gwd.pauseVideo, false);
};
gwd.actions.events.deregisterEventHandlers = function(event) {
gwd.actions.events.removeHandler('gwd-youtube_1', 'playing', gwd.pauseVideo, false);
};
document.addEventListener("DOMContentLoaded", gwd.actions.events.registerEventHandlers);
document.addEventListener("unload", gwd.actions.events.deregisterEventHandlers);
</script>

Categories

Resources