How to play videos using a Flutter application

bhardwaju
Staff

In this article, we'll discuss how to play videos using a Flutter application. We'll use the VideoPlayer plugin to play videos and store them in the device's video gallery.

If you have any questions, please leave a comment below! 

To get started, we need to add the VideoPlayer plugin to our Flutter project. This can be done by adding the following line to the pubspec.yaml file:

 

dependencies:
  video_player: ^0.11.10

 

Once the plugin is installed, we need to create a new VideoPlayerController object. This can be done by adding the following code to the main.dart file:

 

import 'package:video_player/video_player.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: VideoPlayer(
            controller: VideoPlayerController.new(
              VideoPlayer()),
          ),
        ),
      ),
    );
  }
}

 

This code will create a new VideoPlayerController object and set the VideoPlayer object.

Now, we need to add a button to our app that will play the video. This can be done by adding the following code to the main.dart file:

 

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: Button(
            onPressed: () async {
              await VideoPlayerController.new(
                VideoPlayer(),
              ).play();
            },
            child: Text('Play Video'),
          ),
        ),
      ),
    );
  }
}

 

This code will create a new Button object and set the onPressed callback. This callback will play the video using the VideoPlayerController object.

Finally, we need to add the video to our app. This can be done by adding the following code to the main.dart file:

 

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: VideoPlayer(
            controller: VideoPlayerController.new(
              VideoPlayer(),
            ),
            video: Video(
              "https://www.youtube.com/watch?v=U_Yiw4tjeQw",
            ),
          ),
        ),
      ),
    );
  }
}

 

This code will add the video to our app. The Video object will load the video from the YouTube URL.

That's it! We've successfully played a video using a Flutter application.


Authored by:

  • Utkarsh Bhardwaj (@bhardwaju)  Cloud Migration Consultant, Google
  • Shivank Awasthi (@shivankawasthi) Strategic Cloud Engineer, Google

Video by:

  • Amit Dutta (@amitdutta) Strategic Cloud Engineer, Google
2 0 2,243
Authors