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

in #utopian-io6 years ago (edited)

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

Sort:  

Thank you for your contribution. It has been reviewed and scored.

I like the bigger fonts that you have on screen now. It is much easier to follow.

Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

I haven't really changed the fonts since like 2-3 months ago, but I appreciate it. Anyhow, Thank you for moderating my contribution as always.

Did you not zoom into more in some of the screen shots? This is what I am referring. That is what I mean by the fonts showing larger.

If you don't mind, I can do copy writing for for you. FAQ, privacy policy and about us

I appreciate the offer but I do not need any copy writing at this point in time. If I ever do decide that I need some, I will make sure to contact you.

Hey @tensor! Thank you for the great work you've done!

We're already looking forward to your next contribution!

Fully Decentralized Rewards

We hope you will take the time to share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.me/utopian-io

Hi I can't seem to access the deconstructor API. Is there any other API you would recommend for the same purpose?

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 59200.90
ETH 2601.74
USDT 1.00
SBD 2.41