Playing Local, Network and YouTube Videos with the Video Player Plugin in Dart's Flutter Framework

flutter-logo.jpg

What Will I Learn?

  • You will learn how to play local videos using the video player plugin
  • You will learn how to play network videos
  • You will learn how to decode YouTube URLs
  • You will learn about the state class' deactivate method

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic understanding of Videos and Video Players
  • A fair understanding of Imperative or Object Oriented Programming
  • An understanding of basic Mobile Development

Resources for Flutter and this Project:

Sources:

Difficulty

  • Intermediate

Description

This Video Tutorial looks at how we can implement and use the Flutter Video Player to play multiple types of videos in Flutter. We also touch on some other minor elements of the flutter framework such as the deactivate method for the State class.

Outline and Overview

Outline for this Tutorial

Playing videos in Flutter is a fairly popular feature. Many popular mobile applications make use of video players to good effect and it makes sense that developers would want to be able to implement this type of feature in a cross platform framework like flutter. The solution to this problem comes in the form of the Video Player plugin from the FlutterFire repository. This plugin allows flutter to interface with the platform's native video players so that those video players can be embedded into the flutter widget tree.

Item 1: Local Videos

Using local videos in a flutter application is just as easy as making use of a local image, font, or sprite. We can specify the path for the asset through our pubspec.yaml file and then we can directly call to that path in our application. The video player plugin comes with a special constructor which lets us build out a video player controller specifically geared towards playing local videos. By invoking this constructor we can directly call the videos just by passing in the file path as a string.

createVideo.jpg

Here is the createVideo function that we use to setup our video controller. In it you can see the asset constructor in use. The constructor called VideoPlayerController.asset directly calls to the path where our local video is stored.

Item 2: Network and YouTube Videos

A vast majority of mobile applications these days make use of the cloud to minimize their storage footprint. Not only can we play local videos using the video player plugin, we can play network videos. We are able to do this by directly referencing the URL for the video in a special network constructor. If a video can be played inside of the normal mobile video player for either Android or iOS, then it can also be played inside of the flutter video player.

video_api.jpg

Due to the way that YouTube videos are encoded on the server, we can't directly call to a YouTube video URL. We can however, use the YouTube V3 API to decode these URLs into a format that we can make use of in our application. The above image shows one such URL.

Item 3: The Deactivate Method

Most widgets have access to many lifecycle methods like initial state and dispose. The Stateful Widget's accompanying State class also has access to a method called deactivate. This method is a bit like the dispose method, however, it is specific to the state class. Whenever the state object is removed from the widget tree in one area and then reinserted into the tree in another, the deactivate method is called. This allows the user and the framework to more cleanly decouple the state from the UI in these cases.

deactivate-video.jpg

Here is the deactivate method for our application. You can see that we actually mute the video player's volume and then remove the listener from this video player. Because the listener calls the setState function every frame that the video updates in it is much more efficient for use to remove this listener on the event that this state object is removed from this widget.

Project GitHub Repository:

The Source Code for this project can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
7 Comments