Display Videos in Flutter

Nurettin Eraslan
3 min readJun 29, 2023

--

Flutter supports a wide range of media content to provide rich and immersive user experiences. Displaying videos can be a crucial requirement for many mobile applications. The vide_player package allows us to integrate video playback and management functionality quickly and easily in Flutter. In this article, I will guide you step by step on how to use the vide_player package and provide code examples.

Step 1: Adding and Importing the Package: To use the vide_player package for video playback, you need to add it as a dependency in your pubspec.yaml file. Simply add the line “video_player: ^x.x.x” under the dependencies section to include the package in your project.

Next, import the vide_player package in your project as follows: import ‘package:video_player/video_player.dart’;

Step 2: Integrating the Video Player into a Widget : The most common way to use the vide_player package for video playback is by using the VideoPlayer widget. Let’s see how we can play videos by using the VideoPlayer widget within a Container widget.

Here’s a sample code snippet:

dependencies:
flutter:
sdk: flutter
video_player: version_no
Container(
child: VideoPlayer(
VideoPlayerController.asset('assets/video/video.mp4'),
),
)

In the above code snippet, we create a video player by using the VideoPlayerController.asset() method inside the VideoPlayer widget. In this example, a video player is created that plays a video file named ‘assets/video/video.mp4’.

Conclusion: In this article, we explained how to use the vide_player package in Flutter to display videos and provided code examples. We first showed the necessary steps to add and import the package into your project. Then, we demonstrated how to integrate a video player into a Widget using the VideoPlayer widget within a Container widget, providing a code snippet. The vide_player package is a powerful tool for video playback in Flutter applications, and by using this package, you can easily integrate video playback and management functionality into your applications.

Let’s we look on a simple app

Now, let’s move to the main.dart file. Use the following code to create a basic video player application:

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
runApp(VideoPlayerApp());
}

class VideoPlayerApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Video Player',
home: VideoPlayerScreen(),
);
}
}

class VideoPlayerScreen extends StatefulWidget {
@override
_VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;

@override
void initState() {
_controller = VideoPlayerController.asset('assets/video/video.mp4');
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.play();
super.initState();
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Video Player'),
),
body: FutureBuilder(
future: _initializeVideo

The above code creates a simple video player application. The VideoPlayerScreen class is defined as a StatefulWidget, which initializes the video player using the VideoPlayerController class. When the application starts, the video starts playing automatically, and it can be controlled with a play/pause button.

To run the application, use the flutter run command. This will create a basic application where you can play videos.

--

--