media_kit 0.0.5+1 media_kit: ^0.0.5+1 copied to clipboard
A complete video & audio playback library for Flutter & Dart. Performant, stable, feature-proof & modular.
1. Initialize the library
import 'package:flutter/widgets.dart';
import 'package:media_kit/media_kit.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized();
runApp(const MyApp());
}
2. Create a player to play & control an video/audio source with it
import 'package:media_kit/media_kit.dart';
/// Create a [Player] instance for video or audio playback.
final Player player = Player();
/// Subscribe to event streams & listen to updates.
player.streams.playlist.listen((e) => print(e));
player.streams.playing.listen((e) => print(e));
player.streams.completed.listen((e) => print(e));
player.streams.position.listen((e) => print(e));
player.streams.duration.listen((e) => print(e));
player.streams.volume.listen((e) => print(e));
player.streams.rate.listen((e) => print(e));
player.streams.pitch.listen((e) => print(e));
player.streams.buffering.listen((e) => print(e));
/// Open a playable [Media] or [Playlist].
await player.open(Media('file:///C:/Users/Hitesh/Music/Sample.mp3'));
await player.open(Media('file:///C:/Users/Hitesh/Video/Sample.mkv'));
await player.open(Media('rtsp://www.example.com/live'));
await player.open(Media('asset:///videos/bee.mp4'));
await player.open(
Playlist(
[
Media('https://user-images.githubusercontent.com/28951144/229373695-22f88f13-d18f-4288-9bf1-c3e078d83722.mp4'),
Media('https://user-images.githubusercontent.com/28951144/229373709-603a7a89-2105-4e1b-a5a5-a6c3567c9a59.mp4'),
Media('https://user-images.githubusercontent.com/28951144/229373716-76da0a4e-225a-44e4-9ee7-3e9006dbc3e3.mp4'),
Media('https://user-images.githubusercontent.com/28951144/229373718-86ce5e1d-d195-45d5-baa6-ef94041d0b90.mp4'),
Media('https://user-images.githubusercontent.com/28951144/229373720-14d69157-1a56-4a78-a2f4-d7a134d7c3e9.mp4'),
],
),
);
/// Control playback state.
await player.play();
await player.pause();
await player.playOrPause();
await player.seek(const Duration(seconds: 10));
/// Use or modify the queue.
await player.next();
await player.previous();
await player.jump(2);
await player.add(Media('https://www.example.com/sample.mp4'));
await player.move(0, 2);
/// Customize speed, pitch, volume, shuffle, playlist mode, audio device.
await player.setRate(1.0);
await player.setPitch(1.2);
await player.setVolume(50.0);
await player.setShuffle(false);
await player.setPlaylistMode(PlaylistMode.loop);
await player.setAudioDevice(AudioDevice.auto());
/// Release allocated resources back to the system.
await player.dispose();
3. Render video output
GPU powered (hardware accelerated), automatically fallbacks to S/W rendering based on system.
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart'; /// Provides [Player], [Media], [Playlist] etc.
import 'package:media_kit_video/media_kit_video.dart'; /// Provides [VideoController] & [Video] etc.
class MyScreen extends StatefulWidget {
const MyScreen({Key? key}) : super(key: key);
@override
State<MyScreen> createState() => _MyScreenState();
}
class MyScreenState extends State<MyScreen> {
/// Create a [Player].
final Player player = Player();
/// Store reference to the [VideoController].
VideoController? controller;
@override
void initState() {
super.initState();
Future.microtask(() async {
/// Create a [VideoController] to show video output of the [Player].
controller = await VideoController.create(player);
/// Play any media source.
await player.open(Media('https://user-images.githubusercontent.com/28951144/229373695-22f88f13-d18f-4288-9bf1-c3e078d83722.mp4'));
setState(() {});
});
}
@override
void dispose() {
Future.microtask(() async {
/// Release allocated resources back to the system.
await controller?.dispose();
await player.dispose();
});
super.dispose();
}
@override
Widget build(BuildContext context) {
/// Use [Video] widget to display the output.
return Video(
/// Pass the [controller].
controller: controller,
);
}
}