HLS.js Guide and Best Practices
Master the core technology of HLS streaming media playback, including working principles, basic code examples, common configurations, event listening, and error handling.
Read More βBuilding a smooth mobile video experience to improve user satisfaction
With the development of the mobile internet, mobile video viewing has become mainstream. However, mobile video playback faces many challenges: autoplay restrictions, battery consumption, unstable networks, diverse screen sizes, and more. Optimizing mobile video playback is crucial for improving user experience and reducing bounce rates.
This article will comprehensively introduce optimization tips and best practices for mobile video playback from multiple dimensions: autoplay strategy, mute handling, gesture control, orientation switching, performance optimization, and battery optimization.
To prevent web pages from automatically playing videos without the user's knowledge and consuming data, mobile browsers generally impose strict restrictions on autoplay.
On mobile browsers, video autoplay requires meeting one of the following conditions:
Muted autoplay is currently the most commonly used autoplay solution on mobile devices β it can display video content while complying with browser policies.
<video id="video" muted autoplay playsinline webkit-playsinline>
<source src="video.mp4" type="video/mp4">
</video>
muted: Muted β a necessary condition for autoplayautoplay: Autoplayplaysinline: Inline playback, does not enter fullscreen (required for iOS)webkit-playsinline: Compatibility with older iOS SafariAutoplay may fail for various reasons and requires detection and fallback handling:
const video = document.getElementById('video');
const playPromise = video.play();
if (playPromise !== undefined) {
playPromise.then(() => {
console.log('Autoplay succeeded');
}).catch(error => {
console.log('Autoplay failed, showing play button');
showPlayButton();
});
}
A common approach is to start with muted autoplay, then enable sound after the user clicks:
video.addEventListener('click', function() {
if (video.muted) {
video.muted = false;
video.volume = 1;
}
});
Gesture operations are the most natural way to interact on mobile devices. Proper gesture control can greatly improve the playback experience.
let touchStartX = 0;
let touchStartTime = 0;
let isSeeking = false;
video.addEventListener('touchstart', function(e) {
touchStartX = e.touches[0].clientX;
touchStartTime = video.currentTime;
isSeeking = true;
});
video.addEventListener('touchmove', function(e) {
if (!isSeeking) return;
const deltaX = e.touches[0].clientX - touchStartX;
const seekTime = deltaX / window.innerWidth * video.duration * 0.5;
video.currentTime = Math.max(0, Math.min(video.duration, touchStartTime + seekTime));
});
video.addEventListener('touchend', function() {
isSeeking = false;
});
Portrait/landscape switching is a common requirement for mobile video playback. It's important to handle the switching logic and layout adaptation properly.
function isLandscape() {
return window.innerWidth > window.innerHeight;
}
window.addEventListener('resize', function() {
if (isLandscape()) {
console.log('Landscape mode');
enterFullscreen();
} else {
console.log('Portrait mode');
exitFullscreen();
}
});
function enterFullscreen() {
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen();
} else if (video.webkitEnterFullscreen) {
video.webkitEnterFullscreen(); // iOS
}
}
function exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
During fullscreen playback, you can try to lock the screen orientation:
if (screen.orientation && screen.orientation.lock) {
screen.orientation.lock('landscape').catch(function() {
console.log('Unable to lock screen orientation');
});
}
iOS and Android have many differences in video playback that need to be handled specifically.
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
const isAndroid = /Android/.test(navigator.userAgent);
if (isIOS) {
video.setAttribute('playsinline', '');
video.setAttribute('webkit-playsinline', '');
}
if (isAndroid) {
video.setAttribute('x5-video-player-type', 'h5');
video.setAttribute('x5-video-player-fullscreen', 'true');
}
Mobile devices have limited performance, so optimization needs to be done from multiple aspects.
Automatically select the appropriate video quality based on the user's network conditions:
<!-- Only preload metadata to save data -->
<video preload="metadata">...</video>
<!-- Preload more on WiFi -->
<script>
if (navigator.connection && navigator.connection.effectiveType === '4g') {
video.preload = 'auto';
}
</script>
Video playback is a major battery drain on mobile devices. Optimizing battery consumption can improve user satisfaction.
Prioritize hardware decoding to reduce CPU usage:
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
if (!video.paused) {
video.pause();
wasPlaying = true;
}
} else {
if (wasPlaying) {
video.play();
wasPlaying = false;
}
}
});
In scenarios like list pages, use muted autoplay to attract user attention, then enable sound after the user clicks. At the same time, provide a clear play button as a fallback solution.
Implement common gesture operations like swipe fast forward and volume adjustment, allowing users to operate with one hand. Also provide visual feedback so users know the result of their actions.
Automatically enter fullscreen mode in landscape orientation and exit fullscreen in portrait orientation. Ensure good layout and interactive experience in both modes.
Use adaptive bitrate, reasonable preloading, CDN acceleration, and other methods to reduce time to first frame and buffering waits. Provide clear prompts to users in weak network environments.
Respect the user's battery and data usage, remind users on mobile networks, and provide low-quality options. Pause playback when the page is not visible to save resources.
Have friendly prompts and retry mechanisms for network errors, decoding failures, and other situations. Don't let users see blank or black screens.
Mobile video playback optimization is a comprehensive topic involving user interaction, performance, battery, compatibility, and more. Only by deeply understanding the features and limitations of mobile devices can we create a smooth, natural, and beloved video playback experience.
We hope these tips and best practices introduced in this article will help you optimize mobile video playback and improve user satisfaction and retention.
More great articles and useful tools
Master the core technology of HLS streaming media playback, including working principles, basic code examples, common configurations, event listening, and error handling.
Read More βIn-depth explanation of video CDN acceleration principles and optimization methods, including caching strategies, bandwidth control, edge computing, pre-caching, and cost optimization recommendations.
Read More βIn-depth explanation of HLS encryption principles and implementation, including AES-128 and SAMPLE-AES encryption schemes, key management, server configuration, and security best practices.
Read More βUse our free online video player, supporting HLS, FLV, DASH and other formats, mobile-friendly
Try It Now β‘