Youtube_player_flutter: [BUG] Problem with _controller.value.position.inSeconds

Created on 24 Mar 2021  路  2Comments  路  Source: sarbagyastha/youtube_player_flutter

Describe the bug
_controller.value.position.inSeconds does not update when the video is playing. It's always 0

Expected behavior
_controller.value.position.inSeconds should show the current second being played in the player

To Reproduce
This is the small example:

`import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:youtube_player_iframe/youtube_player_iframe.dart';
import 'dart:async';

class NewVideoScreen extends StatefulWidget {
@override
_NewVideoScreenState createState() => _NewVideoScreenState();
}

class _NewVideoScreenState extends State {
YoutubePlayerController _controller;

@override
void initState() {
super.initState();
_controller = YoutubePlayerController(
initialVideoId: 'tcodrIK2P_I',
params: const YoutubePlayerParams(
startAt: const Duration(minutes: 1, seconds: 36),
showControls: false,
showFullscreenButton: true,
desktopMode: true,
privacyEnhanced: true,
),
);

_controller.onEnterFullscreen = () {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);
  print('Entered Fullscreen');
};
_controller.onExitFullscreen = () {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  Future.delayed(const Duration(seconds: 1), () {
    _controller.play();
  });
  Future.delayed(const Duration(seconds: 5), () {
    SystemChrome.setPreferredOrientations(DeviceOrientation.values);
  });
  print('Exited Fullscreen');
};

var numSecs = Duration(seconds:1);
var _timer = new Timer.periodic(numSecs, (Timer t) => showSeconds(t));

}

void showSeconds(t){
print('Position in seconds: ${_controller.value.position.inSeconds} ');
}

@override
Widget build(BuildContext context) {

const player = YoutubePlayerIFrame();
return YoutubePlayerControllerProvider(
  // Passing controller to widgets below.
  controller: _controller,
  child: Scaffold(
    appBar: AppBar(
      title: const Text('Youtube Player Demo'),
    ),
    body: LayoutBuilder(
      builder: (context, constraints) {
        if (kIsWeb && constraints.maxWidth > 800) {
          return Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Expanded(child: player),
            ],
          );
        }
        return ListView(
          children: [
            player,
          ],
        );
      },
    ),
  ),
);

}

@override
void dispose() {
  _controller.close();
  super.dispose();
}

}

`

bug

Most helpful comment

Was able to find a work around.

 _controller.distinct().listen((event) {
      position = event.position;
    });

All 2 comments

Been trying to implement skip function. Was trying to get the current position and add 10 to it so that i could call seekTo method. but position is always 0. @sarbagyastha

Was able to find a work around.

 _controller.distinct().listen((event) {
      position = event.position;
    });
Was this page helpful?
0 / 5 - 0 ratings