media-kit / media-kit

A cross-platform video player & audio player for Flutter & Dart.
https://github.com/media-kit/media-kit
MIT License
893 stars 126 forks source link

the seek function doesn't work #767

Closed Anto-2004 closed 1 month ago

Anto-2004 commented 1 month ago

I did all the possible tests, I even changed the version of flutter, I can't understand I'm passing the example code from the documentation to show the video the only thing I want and set the initial position with the seek method but it doesn't work always starts from scratch. I put the code I have, if anyone can give me some suggestions, I can't understand.

Code: 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.        

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  // Necessary initialization for package:media_kit.
  MediaKit.ensureInitialized();
  runApp(
    const MaterialApp(
      home: MyScreen(),
    ),
  );
}

class MyScreen extends StatefulWidget {
  const MyScreen({Key? key}) : super(key: key);
  @override
  State<MyScreen> createState() => MyScreenState();
}

class MyScreenState extends State<MyScreen> {
  // Create a [Player] to control playback.
  late final player = Player();
  // Create a [VideoController] to handle video output from [Player].
  late final controller = VideoController(player);

void initAll() async {

  player.open(Media('example-url'));

  await player.seek(
    const Duration(
      seconds: 90,
    ),
  );

}

@override
void initState() {
  super.initState();

  initAll();
}

@override
void dispose() {
  player.dispose();
  super.dispose();
}

@override
Widget build(BuildContext context) {
  return Center(
    child: SizedBox(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.width * 9.0 / 16.0,
      // Use [Video] widget to display video output.
      child: Video(controller: controller),
    ),
  );
}

}

23doors commented 1 month ago

Start/end time was implemented in main repo here: https://github.com/media-kit/media-kit/pull/581 It just wasn't released yet. To use it one needs to use dependency_overrides like for example here: https://github.com/media-kit/media-kit/pull/581#issuecomment-1951650794

Anto-2004 commented 1 month ago

I found the solution, since at the moment it is not possible to select the start minute, when they update the library I will change it

await player.open(Media(widget.videoUrl));
controller.player.stream.duration.listen((event) async {
  if (event.inSeconds > 0){
    await player.seek(position);
  }
});