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 streams 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 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('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();
Constructors
- Player({PlayerConfiguration configuration = const PlayerConfiguration()})
- 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
- streams → PlayerStreams
-
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 Mediato
. -
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
astrue
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.
-
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
. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited