text
stringlengths
1
372
to be displayed in a specific aspect ratio, such as 16x9 or 4x3.
therefore, wrap the VideoPlayer widget in an AspectRatio
widget to ensure that the video has the correct proportions.
furthermore, you must display the VideoPlayer widget after the
_initializeVideoPlayerFuture() completes. use FutureBuilder to
display a loading spinner until the controller finishes initializing.
note: initializing the controller does not begin playback.
<code_start>
// use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionstate == ConnectionState.done) {
// if the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
// use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
);
} else {
// if the VideoPlayerController is still initializing, show a
// loading spinner.
return const center(
child: CircularProgressIndicator(),
);
}
},
)
<code_end>
<topic_end>
<topic_start>
5. play and pause the video
by default, the video starts in a paused state. to begin playback,
call the play() method provided by the VideoPlayerController.
to pause playback, call the pause() method.
for this example,
add a FloatingActionButton to your app that displays a play
or pause icon depending on the situation.
when the user taps the button,
play the video if it’s currently paused,
or pause the video if it’s playing.
<code_start>
FloatingActionButton(
onPressed: () {
// wrap the play or pause in a call to `setstate`. this ensures the
// correct icon is shown.
setState(() {
// if the video is playing, pause it.
if (_controller.value.isplaying) {
_controller.pause();
} else {
// if the video is paused, play it.
_controller.play();
}
});
},
// display the correct icon depending on the state of the player.
child: icon(
_controller.value.isPlaying ? icons.pause : icons.play_arrow,
),
)
<code_end>
<topic_end>
<topic_start>
complete example
<code_start>
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() => runApp(const VideoPlayerApp());
class VideoPlayerApp extends StatelessWidget {
const VideoPlayerApp({super.key});
@override
widget build(BuildContext context) {
return const MaterialApp(
title: 'video player demo',
home: VideoPlayerScreen(),
);
}
}
class VideoPlayerScreen extends StatefulWidget {
const VideoPlayerScreen({super.key});
@override
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
late VideoPlayerController _controller;
late future<void> _initializeVideoPlayerFuture;
@override
void initState() {
super.initState();
// create and store the VideoPlayerController. the VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.networkUrl(
uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
),