movie_utils 1.1.1
movie_utils: ^1.1.1 copied to clipboard
A ready-to-use Flutter video player with M3U8/HLS and iOS AirPlay support, and playback completion callback.
movie_utils #
A ready-to-use Flutter video player built on media_kit, with AirPlay support on iOS via flutter_to_airplay.
Key benefits:
- AirPlay on iOS through the native iOS player.
- HLS / m3u8 playback that worked better in our testing than using media_kit directly.
- Ready-to-use playback controls and callbacks, including an optional playback-finished notification.
- Optional desktop-style media_kit controls for keyboard-driven or focus-sensitive layouts, with Arrow Up/Down intentionally omitted to avoid stealing focus-navigation keys and accidental volume changes.
MoviePlayerWidget displays video in the space provided by its parent widget.
On iOS, MoviePlayerWidget uses FlutterAVPlayerView by default for AirPlay support.
Use forceMediaKit: true on iOS if media_kit instead of the native AirPlay-compatible
player should be used.
Use preferDesktopControls when your app needs the default media_kit desktop controls
on other devices than desktop.
Use onPlaybackFinished when your app needs to know that playback has ended,
for example, to advance to the next movie or image in a continuous sequence.
media_kit is selected automatically when any of these are true:
- the app is not running on iOS
forceMediaKit: trueis setpreferDesktopControls: trueis setonPlaybackFinishedis provided
Installation #
Add movie_utils and media_kit to your app:
flutter pub add movie_utils media_kit
Initialization #
Import both packages and initialize media_kit before starting the app:
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:movie_utils/movie_utils.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized();
runApp(const MyApp());
}
Usage #
MoviePlayerWidget fills the bounded space provided by its parent. In a
Scaffold, it uses the available body area:
Scaffold(
body: MoviePlayerWidget(path: videoPath),
)
For inline playback in a smaller area, give it a bounded area, for example:
SizedBox(
height: 240,
child: MoviePlayerWidget(path: videoPath),
)
If placing it in a Column, use Expanded when the parent has a bounded
height. The media_kit renderer preserves the video's aspect ratio by default,
leaving any extra space around the video.
On iOS, MoviePlayerWidget uses FlutterAVPlayerView by default for AirPlay support.
Set forceMediaKit: true to use media_kit instead:
MoviePlayerWidget(
path: videoPath,
forceMediaKit: true,
)
On platforms other than iOS, media_kit is already used, so forceMediaKit has no effect.
The parameter controls the playback backend, not widget layout or navigation.
Use onPlaybackFinished to advance e.g. a continuous sequence of movies and images.
This automatically selects media_kit - explicit forceMediaKit: true is not necessary.
MoviePlayerWidget(
path: videoPath,
onPlaybackFinished: playNextItem,
)
FlutterAVPlayerView does not expose playback-finished control.
If you prefer the desktop-style media_kit controls, set preferDesktopControls: true. This switches to media_kit's desktop control layout for keyboard-driven
or focus-sensitive layouts. The shortcut set intentionally omits Arrow Up/Down,
which Flutter uses for widget focus traversal, so the default focus navigation is
not hijacked and volume hotkeys do not accidentally fire when the video is
focused.
This is a desktop/keyboard-oriented control mode, not a dedicated TV remote control scheme. It is intended to avoid key conflicts, not to replace a custom TV-navigation UX.
MoviePlayerWidget(
path: videoPath,
preferDesktopControls: true,
)
Playback lifecycle #
The playback configuration (path, forceMediaKit, and callbacks) is read when
MoviePlayerWidget is mounted and is not updated while it remains mounted.
To play another movie or change the configuration, replace the widget's state.
When using media_kit, player replacement must be coordinated with disposal of the previous player.
Creating a new player before the previous one has been fully disposed can cause exceptions.
Use onDisposedPlayer to coordinate this replacement. It is called when the widget has disposed
its media_kit player, allowing the application to wait for the disposal process before creating
the next player.
String? _currentPath;
String? _message;
bool _playlistDone = false;
@override
void initState() {
super.initState();
_currentPath = getNextMoviePath();
_playlistDone = _currentPath == null;
}
@override
Widget build(BuildContext context) {
final String? path = _currentPath;
final Widget body;
if (_message != null) {
body = Center(child: Text(_message!));
} else if (_playlistDone || path == null) {
body = const Center(child: Text('Done.'));
} else {
body = MoviePlayerWidget(
key: ValueKey(path),
path: path,
onPlaybackFinished: onPlaybackFinished,
onDisposedPlayer: onDisposedPlayer,
);
}
return SafeArea(
child: Scaffold(
appBar: AppBar(title: const Text('Presentation')),
body: body,
),
);
}
String? getNextMoviePath() {
String? path = ...; // Replace ... with your playlist logic.
// It should return each playlist item in order and return null when there are no more items.
return path;
}
void onPlaybackFinished() {
if (!mounted) return;
setState(() {
_currentPath = null;
_message = 'Please wait a second.';
});
}
void onDisposedPlayer() {
if (!mounted) return;
final String? nextPath = getNextMoviePath();
WidgetsBinding.instance.addPostFrameCallback((_) async {
setState(() {
_currentPath = nextPath;
_playlistDone = nextPath == null;
_message = null;
});
});
}
We use this approach because playlists may contain both movies and images, and recreating
the player makes switching between them straightforward. Additionally, although media_kit
supports changing the movie directly, reusing the player would require additional logic
to handle movie-to-movie and movie-to-image transitions, manage player state across the
widget lifecycle (initState and didUpdateWidget), and reconfigure playback listeners.
Recreating the player keeps the implementation simpler and requires less code.
If media_kit reports a playback error, the player shows an error message for
five seconds and then calls onPlaybackFinished, if provided. Disposing the
widget before the five seconds elapse cancels that callback.
onPlaybackFinished and onDisposedPlayer are only called when using the
media_kit player.
HLS / m3u8 playback experience #
We encountered m3u8 playback issues when using media_kit directly; the setup
in this package resolved the issue in our app.
Other users have reported a similar HLS issue in
media_kit, including a
black screen with audio. The discussion suggests trying playback without
hardware acceleration, but does not confirm a fix; we tried that workaround
as well, and it did not help in our case.
The native iOS player is used for AirPlay support, not as the m3u8 workaround.
Our m3u8 result is based on our own testing, may not apply to other HLS issues
or streams, and does not guarantee playback of every m3u8 stream.
License #
See LICENSE.