kplayer

Flutter player (currently only audio)

online preview: Go live

sopport

windows

because of this issue: github.com/bluefireteam/audioplayers/issues/1119 if want to use kplayer_with_audioplayers, use this on pubspec.yaml:

dependency_overrides:
  audioplayers_windows:
    git:
      url: https://github.com/kflutter/audioplayers
      path: packages/audioplayers_windows
      ref: 263b4cc648d39a79455c221897a2c699f9d1c4c0

Thanks to maintel

macos

on macos if you using just_audio you may need to do some changes

I was able to build the project example when macos/Podfile replace platform :osx, '10.11' to platform :osx, '10.15'. Furthermore, adding to macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements:

<key>com.apple.security.network.client</key>
<true/>

Thanks to Andresit0

specify platform

now u can specify the packages u want to use in every platform dynamically

// by default:
Map<PlatformEnv, PlayerAdaptivePackage?> platforms = {
  PlatformEnv.web: PlayerAdaptivePackage(
    factory: just_audio.Player.new,
    name: 'just_audio',
  ),
  PlatformEnv.ios: PlayerAdaptivePackage(
    factory: just_audio.Player.new,
    name: 'just_audio',
  ),
  PlatformEnv.android: PlayerAdaptivePackage(
    factory: just_audio.Player.new,
    name: 'just_audio',
  ),
  PlatformEnv.windows: PlayerAdaptivePackage(
    factory: audioplayers.Player.new,
    name: 'audioplayers',
  ),
  PlatformEnv.linux: PlayerAdaptivePackage(
    factory: audioplayers.Player.new,
    name: 'audioplayers',
  ),
  PlatformEnv.macos: PlayerAdaptivePackage(
    factory: just_audio.Player.new,
    name: 'just_audio',
  ),
  PlatformEnv.fuchsia: null, // [Hope to add fuchsia support],
};

change or add ur own implementation

learn more here

Player.platforms[PlatformEnv.<platform>] = PlayerAdaptivePackage(
    factory: <custom_package>.Player.new,
    name: 'custom_package',
);

packages has wrapper

  • just_audio (kplayer_with_just_audio)
  • audioplayers (kplayer_with_audioplayers)
  • dart_vlc (kplayer_with_dart_vlc)

Getting Started

main.dart

void main() {
  Player.boot(); //add this  line
  runApp(MyApp());
}

Play from assets:

var player = Player.asset("/assets/sound.mp3");

Play from network:

var player = Player.network("[/assets/sound.mp3](https://example.com/sound.mp3)");

Play from file:

var player = Player.file("C/.../sound.mp3");

Play from bytes: // beta

var player = Player.bytes(fileAsBytes);

or:

var player = Player.create(asset: PlayerMedia.asset("/assets/sound.mp3"), autoPlay: true, once: true)
      ..init()

you have also:

 var palyer = Player.create(asset: PlayerMedia.asset("/assets/sound.mp3"),autoPlay: true, once: true)..init();

// callback
palyer.callback = (PlayerEvent event){
   // just example
   setState((){});
};

// info
var package   = player.package;   // "just_audio" or "dart_vlc"
var position  = player.position;  // setter an getter like seek()
var duration  = player.duration;  // getter
var status    = player.status;    // 
var loop      = player.loop;      // bool
var playing   = player.playing;
...
// streams
player.streams.playing.stream;
player.streams.position.stream;
player.streams.status.stream;
player.streams.volume.stream;
player.streams.speed.stream;
player.streams.loop.stream;

// control
player.play();
player.pause();
player.toggle();
player.stop();
player.seek(newPosition);
player.volume = 0.8; // setter getter
player.speed = 1.2; // Rate
player.loop = true; // looping

//other
player.dispose();
player.player; // the package player instance for more option `dart_vlc`, `audioplayers` , `just_audio`

// all players
PlayerController.players; // List<PlayerController>

// all other players
player.others; // List<PlayerController>

// example pause all other players
player.others.forEach((player) => player.pause());

// Widgets
PlayerBar(player: player, options: []);
PlayerBuilder(
  player: player,
  rebuild: (event, oldEvent) => event != oldEvent, // by default it will rebuild when event changed
  builder: (context, event){
  return // TODO
});

mixins

you can use PlayerStateMixin on State to get full access player state and handle streams dispose automatically

class MyPage extends StatefulWidget {
  final PlayerController player;
  const MyPage({super.key, required this.player});
  @override
  _MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> with PlayerStateMixin {
  @override
  void initState() {
    usePlayer(widget.player);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return // TODO
  }

  @override
  void onPlayingChanged(bool playing) {
    print('onPlayingChanged $playing');
  }
  @override
  void onPositionChanged(Duration position) {
    print('onPositionChanged $position');
  }
  @override
  void onDurationChanged(Duration duration) {
    print('onDurationChanged $duration');
  }
  @override
  void onStatusChanged(PlayerStatus status) {
    print('onStatusChanged $status');
  }
  @override
  void onEvent(PlayerStatus status) {
    print('onStatusChanged $status');
  }
}

QA

how to add options on setting menu?

Just add widgets to player bar options, example:

PlayerBar(player: player, options: [
       SwitchListTile(
         secondary: const Icon(Icons.brightness_2),
         title: const Text("Dark mode"),
       );
])

how to controll auto playing?

use autoPlay param:

var player = Player.asset("/assets/sound.mp3",autoPlay: false);

use only one player

currently you need to dispose the previous player for example:

var player = Player.asset("/assets/sound.mp3",autoPlay: false);
player.play()
// ...
player.dispose();
player = Player.network("www.example.com/file.mp3");

but i plan it to add somthing like player.reuse() to re use player by defrent source also i will add Player.disposeAll() to easly dispose All players

acces to all players

you can acces to all players by PlayerController.players return List<PlayerController>

Source code

check the repository on github (github.com/physia/kflutter/tree/main/kplayer/kplayer)

Check list

  • support playLists
  • add disposeAll
  • add reuse Re-use player for single player use cases
  • add more widget
  • add style option and sub widget

Support ☺️

Coffee for my mind:

E n g o j :)

Libraries

kplayer
patform_info