var skipTime =10; // Time to skip in secondsseekForwardButton.addEventListener('click',function(event) {event.stopPropagation();video.currentTime =Math.min(video.currentTime + skipTime,video.duration);});seekBackwardButton.addEventListener('click',function(event) {event.stopPropagation();video.currentTime =Math.max(video.currentTime - skipTime,0);});
functionlockScreenInLandscape() {if (!('orientation'in screen)) {return; }// Let's force landscape mode only if device is in portrait mode and can be held in one hand.if (matchMedia('(orientation: portrait) and (max-device-width: 768px)').matches) {screen.orientation.lock('landscape'); }}
functionlockScreenInLandscape() {if (!('orientation'in screen)) {return; }// Let's force landscape mode only if device is in portrait mode and can be held in one hand.if (matchMedia('(orientation: portrait) and (max-device-width: 768px)').matches) {screen.orientation.lock('landscape').then(function() {listenToDeviceOrientationChanges(); }); }}
functionlistenToDeviceOrientationChanges() {if (!('DeviceOrientationEvent'in window)) {return; }var previousDeviceOrientation, currentDeviceOrientation;window.addEventListener('deviceorientation',functiononDeviceOrientationChange(event) {// event.beta represents a front to back motion of the device and// event.gamma a left to right motion.if (Math.abs(event.gamma) >10||Math.abs(event.beta) <10) { previousDeviceOrientation = currentDeviceOrientation; currentDeviceOrientation ='landscape';return; }if (Math.abs(event.gamma) <10||Math.abs(event.beta) >10) { previousDeviceOrientation = currentDeviceOrientation;// When device is rotated back to portrait, let's unlock screen orientation.if (previousDeviceOrientation =='landscape') {screen.orientation.unlock();window.removeEventListener('deviceorientation', onDeviceOrientationChange); } } });}
注意:如果网页上有大量视频,并且正在使用Intersection Observer API 来暂停/静音屏幕视频,则可能需要重置视频源,video.src = null。因为它会在无限滚动情况下释放大量资源。
<button id="muteButton"></button>
if ('IntersectionObserver'in window) {// Show/hide mute button based on video visibility in the page.functiononIntersection(entries) {entries.forEach(function(entry) {muteButton.hidden =video.paused ||entry.isIntersecting; }); }var observer =newIntersectionObserver(onIntersection);observer.observe(video);}muteButton.addEventListener('click',function() {// Mute/unmute video on button click.video.muted =!video.muted;});video.addEventListener('volumechange',function() {muteButton.classList.toggle('active',video.muted);});
一次只播放一个视频
如果页面上有多个视频,我建议您只播放一个,并自动暂停其他视频,以便用户不必再听到多个音轨同时播放。
// Note: This array should be initialized once all videos have been added.var videos =Array.from(document.querySelectorAll('video'));videos.forEach(function(video) {video.addEventListener('play', pauseOtherVideosPlaying);});functionpauseOtherVideosPlaying(event) {var videosToPause =videos.filter(function(video) {return!video.paused && video !=event.target; });// Pause all other videos currently playing.videosToPause.forEach(function(video) { video.pause(); });}
if ('mediaSession'in navigator) {navigator.mediaSession.setActionHandler('previoustrack',function() {// User clicked "Previous Track" media notification icon.playPreviousVideo(); // load and play previous video });navigator.mediaSession.setActionHandler('nexttrack',function() {// User clicked "Next Track" media notification icon.playNextVideo(); // load and play next video });}
if ('mediaSession'in navigator) {let skipTime =10; // Time to skip in secondsnavigator.mediaSession.setActionHandler('seekbackward',function() {// User clicked "Seek Backward" media notification icon.video.currentTime =Math.max(video.currentTime - skipTime,0); });navigator.mediaSession.setActionHandler('seekforward',function() {// User clicked "Seek Forward" media notification icon.video.currentTime =Math.min(video.currentTime + skipTime,video.duration); });}