I somehow managed to put two options in one button: when clicking, the video starts to play and enters full screen at the same time.
This is the html:
<video id="video1" class="video-small">
<source src="video/Marinela+Pinguinos-HD.mp4" type="video/mp4" class="video-file">
<source src="video/Marinela_Pinguinos-HD.webm" type="video/webm" class="video-file">
</video>
<button id="play" class="full-play-button" onclick="vidplay(); goFullscreen('video1')">Play fullscreen</button>
JAVASCRIPT:
function vidplay() {
var video = document.getElementById("video1");
var button = document.getElementsByClassName("full-play-button");
if (video.paused) {
video.play();
button.textContent = "||";
} else {
video.pause();
button.textContent = ">";
}
}
function goFullscreen(id) {
// Get the element that we want to take into fullscreen mode
var element = document.getElementById(id);
// These function will not exist in the browsers that don't support fullscreen mode yet,
// so we'll have to check to see if they're available before calling them.
if (element.mozRequestFullScreen) {
// This is how to go into fullscren mode in Firefox
// Note the "moz" prefix, which is short for Mozilla.
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
// This is how to go into fullscreen mode in Chrome and Safari
// Both of those browsers are based on the Webkit project, hence the same prefix.
element.webkitRequestFullScreen();
}
}
So far things go smooth. When entering full screen, there's a default player-like thing at the bottom, with a button offering the possibility to exit full screen.
What I would like to achieve is to be able to pause the video when clicking that button, but I have no idea how.
What I can think of is some kind of a function that detects if we're full screen or not, and if we're not, it would pause/stop (not sure which I prefer yet) the video.
This is what came to my mind, but I'm really a newbie in JS and it doesn't work:
function exitPause() {
var video = document.getElementById("video1");
if (document.exitFullscreen) {
video.pause();
}
else if (document.mozCancelFullScreen) {
video.pause();
}
else if (document.webkitExitFullscreen) {
video.pause();
}
else if (element.msExitFullscreen) {
video.pause();
}
}
What am I doing wrong? How can I make it happen?
Use fullscreenchange event handler:
video.addEventListener(
'fullscreenchange',
function(event) {
if (!document.fullscreenElement) {
video.pause();
}
},
false
);
Note: care for vendor prefixes.
Thanks to Igor Gilyazov I managed to go a bit further. This is how the code looks now:
function vidplay() {
var video = document.getElementById("video1");
var button = document.getElementsByClassName("full-play-button");
video.addEventListener(
'webkitfullscreenchange',
function(event) {
if (!document.fullscreenElement && video.paused) {
video.play();
}
else {
video.pause();
}
},
false
);
}
and going full screen:
function goFullscreen(id) {
// Get the element that we want to take into fullscreen mode
var element = document.getElementById(id);
// These function will not exist in the browsers that don't support fullscreen mode yet,
// so we'll have to check to see if they're available before calling them.
if (element.mozRequestFullScreen) {
// This is how to go into fullscren mode in Firefox
// Note the "moz" prefix, which is short for Mozilla.
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
// This is how to go into fullscreen mode in Chrome and Safari
// Both of those browsers are based on the Webkit project, hence the same prefix.
element.webkitRequestFullScreen();
}
// Hooray, now we're in fullscreen mode!
}
What happens now is when I first click the button, it goes full screen and the video plays. When going back to small screen, it pauses.
This is great.
Unfortunately it happens only every second time (the next time it keeps being paused no matter if going full or small, then right again, then wrong and so on).
I know I'm close, but it's not fully working yet, any ideas for modifications?
Related
I'm trying to get an HTML5 video to play on mobile iOS. Desktop & android work fine.
Clicking the button enters fullscreen mode and plays the video.
I'm seeing reports that some cannot click the button at all though the video thumbnail is loading. The breakdown is currently:
iOS 16.0.2 not working
iOS 15.2.1 works using safari but not firefox or chrome
HTML snippet
`
<div class="video-player">
<video loop="true" width="400px" preload="auto" playsinline="true" controls="true">
<source src="media/video.mp4" type="video/mp4" />
<source src="media/video.webm" type="video/webm" />
<p>Your browser doesn't support HTML5 video. Here is
a link to the video instead </p>
</video>
<button><img src="img/play_video.svg"></button>
</div>
Script in Use
/* Get elements */
var brimmingVideo = document.querySelector('#brimming .video-player video');
var brimmingFullscreen = document.querySelector('#brimming .video-player button');
var brimmingIcon = document.querySelector('#brimming .video-player button img');
/* Build out functions */
// toggle play/pause
function brimmingtogglePlay() {
var method = brimmingVideo.paused ? 'play' : 'pause';
brimmingVideo[method]();
}
// Create fullscreen video button
function brimmingtoggleFullscreen() {
if (brimmingVideo.requestFullScreen) {
brimmingVideo.requestFullScreen();
brimmingtogglePlay();
} else if (brimmingVideo.webkitRequestFullScreen) {
brimmingVideo.webkitRequestFullScreen();
brimmingtogglePlay();
} else if (brimmingVideo.mozRequestFullScreen) {
brimmingVideo.mozRequestFullScreen();
brimmingtogglePlay();
} else if (vid.webkitEnterFullscreen) {
vid.webkitEnterFullscreen();
}
;
brimmingVideo.controls = true;
brimmingVideo.muted = false;
brimmingIcon.className = "hide";
}
// what happens when you exit fullscreen
function brimmingexitHandler() {
if (document.webkitIsFullScreen === false) {
brimmingtogglePlay();
brimmingVideo.muted = true;
brimmingVideo.controls = false;
} else if (document.mozFullScreen === false) {
brimmingtogglePlay();
brimmingVideo.muted = true;
brimmingVideo.controls = false;
} else if (document.msFullscreenElement === false) {
brimmingtogglePlay();
brimmingVideo.muted = true;
brimmingVideo.controls = false;
}
}
/* Hook up event listeners */
// Click events
brimmingFullscreen.addEventListener('click', brimmingtoggleFullscreen);
// Exit fullscreen event
brimmingVideo.addEventListener('fullscreenchange', brimmingexitHandler, false);
brimmingVideo.addEventListener('mozfullscreenchange', brimmingexitHandler, false);
brimmingVideo.addEventListener('MSFullscreenChange', brimmingexitHandler, false);
brimmingVideo.addEventListener('webkitfullscreenchange', brimmingexitHandler, false);
`
Three things are causing this problem:
At javascript, removing return false; of the event listener.
At the stylesheet, the element which calls the action must have the property cursor: pointer;. Probably Apple put this requirement in these calls for best feedback on a user interface.
Again at the stylesheet, we can't set display: none; for hidden input because some browsers don't accept clicks on elements that aren't displayed. (This doesn't apply for you)
Also, the general rule is to "grab" DOM elements only after DOM was loaded!
I'm making a biking simulator where a user can pedal on a device, and their pedaling makes a video play. I want to add a pause screen which pauses the video while the screen is up.
Right now, I have a pause screen that when the div it's in appears, the video pauses, but if the user continues pedaling after the pause screen is up, the video will start to play again in the background.
This is what I'm using to display the pause screen and pause the video. The function gets called when the user pushes a button.
function togglePause() {
var paused = document.getElementById("pauseScreen");
if (paused.style.display == "none") {
paused.style.display = "block";
} else {
paused.style.display = "none";
}
var vid = document.getElementById("video");
vid.pause();
}
The pedaling device works like a bluetooth keyboard, where it sends a keypress events with each pedal rotation.
$(document).keypress(function (event) {
console.log(String.fromCharCode(event.which));
if ('p'.indexOf(String.fromCharCode(event.which)) !== -1) {
video.play();
I'd like to make it so as long as the div is displaying, the video cannot be played. Thanks!
Instead of directly starting the video inside
if ('p'.indexOf(String.fromCharCode(event.which)) !== -1)
you can first check if the div's .display property is set to block and in case it is don't play the video.
if ('p'.indexOf(String.fromCharCode(event.which)) !== -1) {
if (document.getElementById("pauseScreen").style.display != "block") {
video.play();
}
}
I want videos to play/pause when you click on them. Firefox has this behaviour by default. Chrome does not. The simple solution I came up with was setting a click event with jQuery.
var video = $('#myVideo');
var videoDomObj = video.get(0);
//click event for the video itself
video.on('click', function(e){
//when video is clicked it should be paused when playing and vise versa
if (videoDomObj.paused){
videoDomObj.play();
} else{
videoDomObj.pause();
}
});
jsfiddle
This will work in IE and Chrome. However, this will result in a conflict in Firefox, since it will instantly call the default event afterwards and will not play the video at all. e.preventDefault() fixes this problem, but will break all the other controls in Firefox. While they still work, they will play/pause the video. Is there an easy solution for this?
In fact the raw code worked like a charm:
document.getElementById('myVideo').onclick = function(e) {
e.preventDefault();
if (this.paused){
this.play();
} else{
this.pause();
}
}
The jQuery equivalent should be:
jQuery('#myVideo').on('click', function(e) {
e.preventDefault();
if (this.paused){
this.play();
} else{
this.pause();
}
});
But as you said that breaks the video controlls, desired and simplest solution for this could be selecting a parent element (or adding a container, if required):
var video = document.getElementById('myVideo');
video.parentElement.onclick = function(e) {
if (video.paused){
video.play();
} else{
video.pause();
}
}
jQuery:
var $video = jQuery('#myVideo'), video = $video.get(0);
$video.parent().on('click', function(e) {
if (video.paused){
video.play();
} else{
video.pause();
}
});
The only solution I found was to detect the browser for this special case, since the problem only occurs in firefox and it has this feature by default anyway.
jsfiddle
//jQuery video element
var video = $('#myVideo');
//DOM element for HTML5 media events
var videoDomObj = video.get(0);
//detect if firefox
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
//only bind click event if not firefox to prevent broken controls - firefox pauses/plays videos on click by default anyway
if (!is_firefox){
video.on('click', function(e){
//when video is clicked it should be paused when playing and vise versa
if (videoDomObj.paused){
videoDomObj.play();
} else{
videoDomObj.pause();
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="myVideo" width="100%" controls>
<source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
I'm trying to create a small project with video sprites, modeled after this JSFiddle for audio sprites.
Playback works as expected: clicking on the relevant buttons play the relevant portions of the video.
Now, however, I would like to incorporate something that would make the video play in full screen (or full window) when the button is pressed or when a key is pressed. The demo here, for example shows an approach where if you click Enter while the video is playing, it will enter full-screen mode.
I'm not particularly experienced with JavaScript, so it's likely that the solution is staring me right in the face on how to integrate the approach shown in the Mozilla article, but I'm stumped.
Here's what I have right now, which creates the video sprites, as expected:
var videoSprite = document.getElementById('bbb');
// sprite data
var spriteData = {
full: {
start: 0,
length: 595
},
tentotwenty: {
start: 10,
length: 10
},
tentothirty: {
start: 10,
length: 20
},
fiftytoonefifty: {
start: 50,
length: 200
}
};
// current sprite being played
var currentSprite = {};
// time update handler to ensure we stop when a sprite is complete
var onTimeUpdate = function() {
if (this.currentTime >= currentSprite.start + currentSprite.length) {
this.pause();
}
};
videoSprite.addEventListener('timeupdate', onTimeUpdate, false);
// in mobile Safari, the first time this is called will load the audio. Ideally, we'd load the audio file completely before doing this.
var playSprite = function(id) {
if (spriteData[id] && spriteData[id].length) {
currentSprite = spriteData[id];
videoSprite.currentTime = currentSprite.start;
videoSprite.play();
}
};
<video id="bbb">
<source src="https://ia700408.us.archive.org/26/items/BigBuckBunny_328/BigBuckBunny_512kb.mp4" type="video/mp4" />
</video>
<br />
<br />
<ul>
<li>
<button onclick="playSprite('full');">Play full video</button>
</li>
<li>
<button onclick="playSprite('tentotwenty');">Play from 10 to 20 seconds</button>
</li>
<li>
<button onclick="playSprite('tentothirty');">Play from 10 to thirty seconds</button>
</li>
<li>
<button onclick="playSprite('fiftytoonefifty');">Play from 50 to 200 seconds</button>
</li>
</ul>
Any tips on how to extend this to go full screen or full window would be greatly appreciated!
I use the code which exists in MDN and modify it to have toggle full screen it means when press enter the video can be full screen if it is not and reverse that.
document.addEventListener("keydown", function(e) {
if (e.keyCode == 13) {
toggleFullScreen();
}
}, false);
function toggleFullScreen() {
var state = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
if (!state) {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}
You only need to call toggleFullScreen() function when buttons is clicked.
When I press enter it restarts the video. Why??
When you click on the buttons(you focus on that button), the video makes full screen and when you press enter again the video exit from full screen mode and then the focused element(the button which was already clicked) click again so it restarts the video.
Now, I understand what happening. What is the solution?
You only need to call blur() function to remove focus from the element.
HTML
<button onclick="playSprite(this,'full');">Play full video</button>
<button onclick="playSprite(this,'tentotwenty');">Play from 10 to 20 seconds</button>
<button onclick="playSprite(this,'tentothirty');">Play from 10 to thirty seconds</button>
<button onclick="playSprite(this,'fiftytoonefifty');">Play from 50 to 200 seconds</button>
Javascript
function(currentElement,id) {
currentElement.blur();
//your code
}
What is this?
Every time, when playSprite function is called(playSprite(this, YourdesireTime)), the current clicked element pass to function to understand which button is clicked and remove focus from the clicked element.
What is different between your answer and #cviejo's answer?
My answer
has toggling functionality
doesn't reset the video
doesn't optimize(I think you don't like to change your code)
#cviejo's answer
optimize your code
Note: I don't want to say #cviejo's answer is not good because he really minimize your code.
Conclusion
You can combine my code and #cviejo's code for getting better result.
Codepen
I am requesting full screen on button click for <video>, but the thing is that the play() function does not work when requestFullScreen() is used.
Any idea why not?
Code:
video[0].play();
if(video[0].requestFullScreen) {
video[0].requestFullScreen();
} else if(video[0].mozRequestFullScreen) {
video[0].mozRequestFullScreen();
} else if(video[0].webkitRequestFullScreen) {
video[0].webkitRequestFullScreen();
}