A video is an important form of media that can be used in the application. In Flutter, videos are handled through the use of video_player plugin. This performs tasks like playing a video, pausing a video, or muting the same. It can be used to play videos from the internet or the videos stored in the assets of the application. In this article, we will explore the same in detail through an example application.
To build a simple app that can play videos using the below steps:
Add the video_player dependency to pubspec.yaml file.
Give the application permissions to access videos.
Add a VideoPlayerController
Display & play the video.
Now, let’s explore these steps in detail.
Adding video_player plugin:
To add the video_player plugin to the flutter app, open the pubspec.yaml file and add the video_palyer dependency as shown below:
To stream videos from the internet the app will be needing correct set of configuration. Depending upon the OS of the device we can set the permissions as shown below.
For Android devices, the permission to stream videos from the internet can be added by going into the Androidmanifest.xml file at
<project root>/android/app/src/main/AndroidManifest.xml. And add the below lines write after the <application> definition :
Definition of the Flutter Application....
For iOS devices, the permissions can be given by adding the following to the Info.plist file which is located at <project root>/ios/Runner/Info.plist as shown:
The VideoPlayerController facilitates the video playback and control of the video. It establishes the connection to the video and prepare the controller for playback. The Controller that we will be creating here will be a StatefulWidget with a state class. We will initialize the controller using the initState method as shown below:
The VideoPlayer widget from the video_player plugin is used in flutter to display a video. To control the Aspect ratio of the video, we will wrap it inside a AspectRatio Widget. We will also be adding a FloatingActionButton to control the play and pause of the video as shown below:
Please Login to comment...