Player class

Player

Player class provides high-level abstraction for media playback. Large number of features have been exposed as class methods & properties.

The instantaneous state may be accessed using the state getter & subscription to the them may be made using the stream available.

Call dispose to free the allocated resources back to the system.

import 'package:media_kit/media_kit.dart';

MediaKit.ensureInitialized();

// Create a [Player] instance for audio or video playback.

final player = Player();

// Subscribe to event stream & listen to updates.

player.stream.playlist.listen((e) => print(e));
player.stream.playing.listen((e) => print(e));
player.stream.completed.listen((e) => print(e));
player.stream.position.listen((e) => print(e));
player.stream.duration.listen((e) => print(e));
player.stream.volume.listen((e) => print(e));
player.stream.rate.listen((e) => print(e));
player.stream.pitch.listen((e) => print(e));
player.stream.buffering.listen((e) => print(e));

// Open a playable [Media] or [Playlist].

await player.open(Media('asset:///assets/videos/sample.mp4'));
await player.open(Media('file:///C:/Users/Hitesh/Music/Sample.mp3'));
await player.open(
  Playlist(
    [
      Media('file:///C:/Users/Hitesh/Music/Sample.mp3'),
      Media('file:///C:/Users/Hitesh/Video/Sample.mkv'),
      Media('https://www.example.com/sample.mp4'),
      Media('rtsp://www.example.com/live'),
    ],
  ),
);

// 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();

Constructors

Player({PlayerConfiguration configuration = const PlayerConfiguration(), PlatformPlayer? platformPlayer})
Player

Properties

handle → Future<int>
Internal platform specific identifier for this Player instance.
no setter
hashCode → int
The hash code for this object.
no setterinherited
platform ↔ PlatformPlayer?
Platform specific internal implementation initialized depending upon the current platform.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
state → PlayerState
Current state of the Player.
no setter
stream → PlayerStream
Current state of the Player available as listenable Streams.
no setter
streams → PlayerStream
Current state of the Player available as listenable Streams.
no setter

Methods

add(Media media) → Future<void>
Appends a Media to the Player's playlist.
dispose() → Future<void>
Disposes the Player instance & releases the resources.
jump(int index) → Future<void>
Jumps to specified Media's index in the Player's playlist.
move(int from, int to) → Future<void>
Moves the playlist Media at from, so that it takes the place of the Media to.
next() → Future<void>
Jumps to next Media in the Player's playlist.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
open(Playable playable, {bool play = true}) → Future<void>
Opens a Media or Playlist into the Player. Passing play as true starts the playback immediately.
pause() → Future<void>
Pauses the Player.
play() → Future<void>
Starts playing the Player.
playOrPause() → Future<void>
Cycles between play & pause states of the Player.
previous() → Future<void>
Jumps to previous Media in the Player's playlist.
remove(int index) → Future<void>
Removes the Media at specified index from the Player's playlist.
screenshot({String? format = 'image/jpeg', bool includeLibassSubtitles = false}) → Future<Uint8List?>
Takes the snapshot of the current video frame & returns encoded image bytes as Uint8List.
seek(Duration duration) → Future<void>
Seeks the currently playing Media in the Player by specified Duration.
setAudioDevice(AudioDevice audioDevice) → Future<void>
Sets the current AudioDevice for audio output.
setAudioTrack(AudioTrack track) → Future<void>
Sets the current AudioTrack for audio output.
setPitch(double pitch) → Future<void>
Sets the relative pitch of the Player. Defaults to 1.0.
setPlaylistMode(PlaylistMode playlistMode) → Future<void>
Sets playlist mode.
setRate(double rate) → Future<void>
Sets the playback rate of the Player. Defaults to 1.0.
setShuffle(bool shuffle) → Future<void>
Enables or disables shuffle for Player. Default is false.
setSubtitleTrack(SubtitleTrack track) → Future<void>
Sets the current SubtitleTrack for subtitle output.
setVideoTrack(VideoTrack track) → Future<void>
Sets the current VideoTrack for video output.
setVolume(double volume) → Future<void>
Sets the playback volume of the Player. Defaults to 100.0.
stop() → Future<void>
Stops the Player. Unloads the current Media or Playlist from the Player. This method is similar to dispose but does not release the resources & Player is still usable.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited