text
stringlengths
1
372
}
<code_end>
<topic_end>
<topic_start>
play and pause a video
playing videos is a common task in app development,
and flutter apps are no exception. to play videos,
the flutter team provides the video_player plugin.
you can use the video_player plugin to play videos
stored on the file system, as an asset, or from the internet.
warning warning
at this time,
the video_player plugin doesn’t work with any desktop platform.
to learn more, check out the video_player package.
on iOS, the video_player plugin makes use of
AVPlayer to handle playback. on android,
it uses ExoPlayer.
this recipe demonstrates how to use the video_player package to stream a
video from the internet with basic play and pause controls using
the following steps:
<topic_end>
<topic_start>
1. add the video_player dependency
this recipe depends on one flutter plugin: video_player.
first, add this dependency to your project.
to add the video_player package as a dependency, run flutter pub add:
<topic_end>
<topic_start>
2. add permissions to your app
next, update your android and ios configurations to ensure
that your app has the correct permissions to stream videos
from the internet.
<topic_end>
<topic_start>
android
add the following permission to the AndroidManifest.xml file just after the
<application> definition. the AndroidManifest.xml file is found at
<project root>/android/app/src/main/AndroidManifest.xml.
<topic_end>
<topic_start>
iOS
for iOS, add the following to the info.plist file found at
<project root>/ios/Runner/Info.plist.
warning warning
the video_player plugin can only play asset videos in iOS simulators.
you must test network-hosted videos on physical iOS devices.
<topic_end>
<topic_start>
3. create and initialize a VideoPlayerController
now that you have the video_player plugin installed with the correct
permissions, create a VideoPlayerController. the
VideoPlayerController class allows you to connect to different types of
videos and control playback.
before you can play videos, you must also initialize the controller.
this establishes the connection to the video and prepare the
controller for playback.
to create and initialize the VideoPlayerController do the following:
<code_start>
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',
),
);
_initializeVideoPlayerFuture = _controller.initialize();
}
@override
void dispose() {
// ensure disposing of the VideoPlayerController to free up resources.
_controller.dispose();
super.dispose();
}
@override
widget build(BuildContext context) {
// complete the code in the next step.
return container();
}
}
<code_end>
<topic_end>
<topic_start>
4. display the video player
now, display the video. the video_player plugin provides the
VideoPlayer widget to display the video initialized by
the VideoPlayerController.
by default, the VideoPlayer widget takes up as much space as possible.
this often isn’t ideal for videos because they are meant