avatarConstantin Stan

Summary

The article presents a method for embedding video playback functionality in Flutter web applications using the Afterglow video player, as an alternative to the video_player package which lacks full web support.

Abstract

The article addresses the challenge of video playback in Flutter web applications by offering a solution that involves modifying the index.html file to include the Afterglow video player, a lightweight and customizable HTML5 video player. The process is broken down into three steps: editing the index.html to load the Afterglow player, writing a Dart function to interact with the HTML elements and pass the video URL, and triggering the video playback which overlays the Flutter app. The Afterglow player supports various controls and can be customized further or used to play YouTube or Vimeo videos by adjusting the Dart function. The article also suggests that any HTML/JS video player could be integrated in a similar manner and provides a reference to a related article for audio playback in Flutter web applications.

Opinions

  • The author prefers the Afterglow player due to its simplicity and lightweight nature, based on their experience using it in a project.
  • The video_player package's lack of full web support is acknowledged, with the Afterglow player presented as a viable alternative.
  • The Afterglow player is praised for its responsiveness and the range of controls it offers, including play, pause, volume, scroll, and fullscreen options.
  • The article implies that developers should consider the web platform's capabilities and limitations when integrating video playback in Flutter web apps.
  • The author suggests that the method described is versatile, as it can be adapted for portrait videos and other video players, emphasizing the flexibility of the solution provided.

Flutter Web Video Player Alternative

Playing video in a Flutter web application

Big Buck Bunny is CC-BY Blender Foundation 2008

In order to play videos on iOS or Android using Flutter, most people will turn to the video_player package.

Unfortunately, this package is not yet fully and out of the box supported on the web platform.

Before the 10th of December 2019, the method provided in this article was one of the only ways of playing videos in a Flutter web app. At Flutter Interact 2019 the web support for a couple of packages, including the video_player_web package. The options presented bellow still work and may serve you better in your use case.

We’ll explore a solution that enables us to play videos in 3 simple steps:

1.We need to edit the default index.html template file that is located in the web folder. With these edits, we load the Afterglow video player and put in place a trigger, the a HTML element, and a video HTML element that will act as a placeholder for the video we want to play.

This is how our index.html will look like:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Web Video Player</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script></head>
  <body>
    <script src="main.dart.js" type="application/javascript"></script>
    <a id="triggerVideoPlayer" class="afterglow" href="#videoPlayer"></a>
    <video id="videoPlayer" width="960" height="540" data-skin="dark">
    </video>
  </body>
</html>

2. We then write a Dart function named playVideo that will interact with the index.html page, using the universal_html package, and pass on the video URL we want to play:

import 'package:flutter/foundation.dart';
import 'package:universal_html/html.dart' as html;
void playVideo(String atUrl) {
  if(kIsWeb) {
    final v = html.window.document.getElementById('videoPlayer');
    if(v != null) {
      v.setInnerHtml(
        '<source type="video/mp4" src="$atUrl">',
        validator: html.NodeValidatorBuilder()
        ..allowElement('source', attributes: ['src', 'type']));
      final a = html.window.document.getElementById( 'triggerVideoPlayer' );
      if(a != null) {
        a.dispatchEvent(html.MouseEvent('click'));
      }
    }
  } else {
    // we're not on the web platform
    // and should use the video_player package
  }
}

The code takes a reference of the videoPlayer element and, if it exists, sets its inner HTML to accommodate the video URL we passed using the atUrl parameter. Then we get a reference on the triggerVideoPlayer element and we trigger a mouse click.

3. The video will play as an overlay on top of the entire Flutter app. The player is responsive and offers controls for play, pause, volume, scroll, fullscreen and also shows playback info.

playVideo('http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4');

If you need to play portrait videos (or play videos in portrait mode) you can swap the width with height in step 1, or dynamically do that by modifying the playVideo function.

The Afterglow player can be further customized. It can also play YouTube or Vimeo videos just by sending the video ID as a parameter to a bit modified method on step 2.

import 'package:flutter/foundation.dart';
import 'package:universal_html/html.dart' as html;
void playHostedVideo(String withId, [bool isVimeo=false]) {
  if(kIsWeb) {
    final v = html.window.document.getElementById('videoPlayer');
    if(v != null) {
      if(isVimeo) {
        v.setAttribute("data-vimeo-id", withId);
      } else {
        v.setAttribute("data-youtube-id", withId);
      }
      final a = html.window.document.getElementById( 'triggerVideoPlayer' );
      if(a != null) {
        a.dispatchEvent(html.MouseEvent('click'));
      }
    }
  } else {
    // we're not on the web platform
    // and should use the video_player package
  }
}

And step 3 becomes:

// for playing YouTube video
playHostedVideo('aqz-KE-bpKQ');
// or for playing Vimeo video
playHostedVideo('1084537', true);

As a bottom note, any HTML/JS video player can be used like this. I chose Afterglow after using it in a recent project and I liked its simplicity and the fact that its lightweight.

If you’re interested in playing only audio you can check this out:

Tha(nk|t’)s all!

Flutter
Dart
Web Development
Programming
Recommended from ReadMedium