How can I detect when a video on iOS is closed? I am running videojs which launches HTML5 videos as native video players. In order to react properly, I want to get an event when the native player is closed.
There are several similar questions to this one around here, but non of the answers work.
First solution I tried:
player.addEventListener('webkitendfullscreen', onVideoEndsFullScreen, false);
Solution was proposed in 2012 here: How to figure out when a HTML5 video player enters the full screen mode on iOS / iPads?
This method doesn't work for me. The event doesn't get fired (at least in iOS simulator) and I can't do anything with it.
Second solution I tried
// Do on resize
if(video.webkitDisplayingFullscreen == false){
// Exit was triggered
}
Solution was proposed even earlier than 2012 here: Is there an "onClose" event for the fullscreen video player on iPhone?
This method also doesn't work, the video element does not have this attribute (at least in iOS simulator). BTW, this method is deprecated.
Does anyone have an idea about how to get notified about iOS leaving fullscreen nowadays?
You may well have found your solution by now, but I was having the same issue on iPad and iPhone. I found that none of the fullscreenchange events were firing on these devices, though it worked fine elsewhere.
I found my solution at http://html5wood.com/html5-video-fullscreen-change-events-on-ipad/, but I'll explain here for completeness too:
In addition to the various other event listeners for fullscreenchange, I added
var video = document.getElementById(myVideo);
video.addEventListener("webkitendfullscreen", function(){
videoExitedFullscreen(video);
}, false);
(Note that the event listener is called on the video itself, not on the document like the other event listeners could be)
Within that, I'm calling another function that tests if the video is currently fullscreen or not, and makes changes accordingly - I created this as a function so I could easily call it from within each of the multiple event listeners needed for various browsers
(if you're not sure what these are see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API#Prefixing)
//function to be run when full screen is closed
function videoExitedFullscreen(videoElement){
//check if an element is currently full screen
if(document.fullScreenElement || document.webkitIsFullScreen == true || document.mozFullScreen || document.msFullscreenElement ){
//do whatever here if the video is now (just became) full screen
} else {
console.log('fullscreen was closed');
//do whatever you want on fullscreen close, like pause or mute
}
}
Listen for the fulscreenchange event and check isFullscreen().
<link href="http://vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/4.12/video.js"></script>
<video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="640" height="268">
<source src="http://vjs.zencdn.net/v/oceans.mp4" type='video/mp4'>
<source src="http://vjs.zencdn.net/v/oceans.webm" type='video/webm'>
</video>
<script>
videojs('my_video_1').ready(function(){
var player = this;
player.on('fullscreenchange', function(){
if(!player.isFullscreen()) {
alert('Exited fullscreen');
}
});
});
</script>
Related
I have some kind of a strange problem. I try to create a website with a looped background video. The code looks like this one:
<video src="video/bg.mp4" style="z-index: -1;object-fit: cover;" poster="video/bg.jpg" autobuffer autoplay loop muted></video>
This works perfectly fine on most browsers (IE struggles with this object-fit thing but I don't mind) but on iPhone the video won't autoplay but on iPad it does. I already read the New Policies for iOS and I think I meet the requirements (otherwise iPad won't autoplay). I did some other testing:
Removing overlaying divs won't fix it
Removing z-index won't fix it
Wifi or Cellular doesn't make a difference
Video filesize doesn't make a difference, too
Am I doing it wrong or does iPhone simply won't autoplay videos and always requires interaction? I only care for iOS 10, I know that the requirements were different on iOS 9
Does playsinline attribute help?
Here's what I have:
<video autoplay loop muted playsinline class="video-background ">
<source src="videos/intro-video3.mp4" type="video/mp4">
</video>
See the comment on playsinline here: https://webkit.org/blog/6784/new-video-policies-for-ios/
iOs 10+ allow video autoplay inline. but you have to turn off "Low power mode" on your iPhone.
Here is the little hack to overcome all the struggles you have for video autoplay in a website:
Check video is playing or not.
Trigger video play on event like body click or touch.
Note: Some browsers don't let videos to autoplay unless the user interacts with the device.
So scripts to check whether video is playing is:
Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
get: function () {
return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
}});
And then you can simply autoplay the video by attaching event listeners to the body:
$('body').on('click touchstart', function () {
const videoElement = document.getElementById('home_video');
if (videoElement.playing) {
// video is already playing so do nothing
}
else {
// video is not playing
// so play video now
videoElement.play();
}
});
Note: autoplay attribute is very basic which needs to be added to the video tag already other than these scripts.
You can see the working example with code here at this link:
How to autoplay video when the device is in low power mode / data saving mode / safari browser issue
I had a similar problem and I tried multiple solution. I solved it implementing 2 considerations.
Using dangerouslySetInnerHtml to embed the <video> code. For example:
<div dangerouslySetInnerHTML={{ __html: `
<video class="video-js" playsinline autoplay loop muted>
<source src="../video_path.mp4" type="video/mp4"/>
</video>`}}
/>
Resizing the video weight. I noticed my iPhone does not autoplay videos over 3 megabytes. So I used an online compressor tool (https://www.mp4compress.com/) to go from 4mb to less than 500kb
Also, thanks to #boltcoder for his guide: Autoplay muted HTML5 video using React on mobile (Safari / iOS 10+)
I had the same problem - the video not play on iOS. I tried all the code options "playsinline autoplay loop muted". The problem was the video I received was in the wrong mp4 codec. So what helped us, was to upload the video to Vimeo and download the HD Version again. The video is now playing on all mobile devices.
You could also try to use mpeg streamclip. Here is a screenclip of VLC - those are the correct settings. Hope someone does not have to spend 2 hours searching for the problem - happy holidays
Here is a simple solution to auto-play the video in IOS, I've already tried and it is perfectly working on IOS, Android, also on all the browsers on various platforms.
simply use (data-wf-ignore) and (data-object-fit) Attributes for the video and data-wf-ignore for the source tag..
You can see the working example with code here at this Snippet:
<video id="myVideo" autoplay="" muted="" playsinline="" data-wf-ignore="true" data-object-fit="cover">
<source src="https://www.w3schools.com/html/mov_bbb.mp4" data-wf-ignore="true" />
</video>
i try to play a DM video on a page on clicking a custom button. It works perfectly on desktop browsers but not on ios devices.
Here is how i proceed using js sdk and jquery.
function _on_apiready(){
$custom_button.one("click", _playVideo)
}
function _playVideo(e){
player.play();
$custom_button.click(function(){player.togglePlay();})
}
var player = DM.player(dom_el,
{
video:dm_id,
params:{html:1, autoplay:0, info:0, logo:0, related:0},
events:{
apiready: _on_apiready,
timeupdate: _on_progress,
playing: _on_playing,
pause: _on_pause
}
}
);
On ios devices, the video seems to load but doesn't play. I need to press the play button of the player to start the video and then may use my custom button to togglePlay.
Doing something wrong ?
Thx
this question has been answered here already: Dailymotion embedded player on iOS devices (HTML5)
Basically, mobile devices prevent videos from being played automatically, this is why it's not playing with your code. As you say, those devices require an user interaction to first play the video.
I’m developing an iOS app with a looping video embedded into a div. I’ve got the “webkit-playsinline” working so it prevents apple’s fullscreen video player from kicking in… but there’s a glitch. As soon as the video begins its first loop, for some reason it forgets that its set to play inline and goes fullscreen.
Here’s my workaround in case this is frustrating anyone else out there:
<video class=“myVideo” width="640" height="360" autoplay webkit-playsinline>
<source src=“video.mp4" type="video/mp4" />
</video>
Rather than adding the “loop” attribute to the video tag, I use jQuery’s .on function to play the video again when it ends like this:
var myVideo = document.getElementsByClassName('myVideo')[0];
$('.myVideo').on('ended',function(){
myVideo.play();
$('.myVideo').attr('webkit-playsinline', '');
});
I also reminded the video that we’re playing inline just incase it gets any funny ideas and tries to run in full screen mode on replay… viola! problem solved ;)
I'm not sure what the problem is. I have a video that I'd like to play in a modal dialog.
I have everything set up and working fine on all devices. The only issue that I've been unable to overcome is that the video will not play a second time on iOS devices.
http://c4sandbox.com/video/index.html is a simple demonstration of the problem that I'm having. If you close the dialog (it will auto close when the video ends) then click the 'show again' link on an iOS device, then the video player is just an empty black box.
What am I missing? This happens in Safari and Chrome, but only on iOS.
EDIT: The problem appears to be with video.js because a straight html5 video tag will play the video multiple times as expected. Unfortunately, I need the flash fallback so html5 only is not an option.
I'm having a similar issue. The way I am getting around it is using the player's dispose() method to kill the instance and re-injecting the HTML for the player and re-instantiating it.
Thanks #Victor! your solution works. Since there is no example code given, here is the code I used.
//Init
videoPlayer = _V_("video_post", {
controls:true,
preload:"auto",
autoplay:true,
}, function(){
});
after you're done with the player (e.g. closes the video dialog), dispose the player
videoPlayer.dispose();
Done. hope this helps.
I am currently developing an iPad app that aggregates various video feeds on the internet. Then using a UIWebView, some Javascript, a <video> tag, and the mp4 URL I can playback the video and the native iOS video controls appear. This all works great.
Additionally by subscribing to the video 'end' event, I can queue up another video to play after the one you were watching finishes. This also works great, EXCEPT if you were in fullscreen when the next video starts to play, you are taken out of it and back to the app!
Does anyone know a way that I can persist fullscreen HTML5 video in iOS when a new video is loaded?
The following worked for me on mobile safari on an iPod touch. Listen for the end event and change the source of the current video tag to the new video.
<html>
<body>
<video id="myVideo" src="http://shapeshed.com/examples/HTML5-video-element/video/320x240.m4v" autoplay>
</video>
<script>
document.getElementById('myVideo').addEventListener('ended',myHandler,false);
function myHandler(e) {
if(!e) { e = window.event; }
// What you want to do after the event
document.getElementById('myVideo').src='http://movies.apple.com/media/us/html5/showcase/2010/demos/apple-html5-demo-tron_legacy-us-20100601_r848-2cie.mov';
}
</script>
</body>
</html>