Explain Codes LogoExplain Codes Logo

Html5 element on Android

html
responsive-design
video-tag
mobile-interaction
Nikita BarsukovbyNikita Barsukov·Oct 16, 2024
TLDR

To ensure your <video> element works on Android, encode your video using H.264 and audio with AAC, then embed like this:

<video controls> <source src="path/to/video.mp4" type="video/mp4"> </video>

Your server should support range requests (indicated by Accept-Ranges: bytes header) for seamless streaming and seeking.

<video controls> <source src="path/to/video.mp4"> </video>

Invoke video.play() via JavaScript for better control over when the playback starts:

// Play time! document.querySelector('video').play();

Utilize a tool like Handbrake with the iPhone preset and 'Web Optimized' option checked for universal compatibility.

User experience and functionality

Mobile interaction enhancements

To improve user experience on mobile devices like Android, consider setting a poster image to give users a preview before the video starts:

<video controls poster="path/to/poster.jpg"> <source src="path/to/video.mp4"> </video>

Increase immersion by launching videos in fullscreen on a user’s click:

// Let's go to the cinema document.querySelector('video').addEventListener('click', function() { if (this.requestFullscreen) { this.requestFullscreen(); } else { // Looks like we're staying home this.play(); } });

Providing smooth video rendering

Enable streamlined user experience by setting explicit width and height attributes on <video>:

<video controls width="640" height="360" poster="path/to/poster.jpg"> <source src="path/to/video.mp4"> </video>

The controls attribute provides access to the browser's built-in player controls, while the onload event can initialize settings:

<video controls onload="init()"> <source src="path/to/video.mp4"> </video>

Enhancing via JavaScript

For extra functionality, like custom play controls, define JavaScript functions:

<head> <script> // Define custom control functions here function playVideo() { var video = document.getElementById('myVideo'); // Hey, no skipping the ads! video.play(); } // more control functions... </script> </head>

Override the onclick event of the video tag for custom actions:

<video id="myVideo" controls onclick="playVideo();"> <source src="path/to/video.mp4"> </video>

For broader browser support, utilize addEventListener rather than attachEvent.

Smooth playing and troubleshooting

Testing and codec compatibility

Widely supported codecs like H.264 provide the best compatibility, but testing across multiple devices is essential:

- Use HTML5 test pages for supported video codecs on specific Android devices - Stay informed on new browser and device developments for future codec support

Adaptive streaming and network considerations

Use the preload attribute sparingly to ensure optimal load times:

<video controls preload="metadata"> <source src="path/to/video.mp4"> </video>

For adaptive streaming, consider technologies like HLS or DASH, designed for a good streaming experience over varied network conditions.

Autoplay policies

Autoplay policies can prevent videos from playing automatically. So, avoid relying on autoplay. Instead, provide a play button for users to initiate video playback.