Skip to content

Commit

Permalink
Fix for #108 and #113
Browse files Browse the repository at this point in the history
  • Loading branch information
sarbagyastha committed Nov 15, 2019
1 parent a064b17 commit f9806b8
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 66 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.0.2
* Added `onEnded` callback for `YoutubePlayer` widget. (Fixes [#108](https://github.com/sarbagyastha/youtube_player_flutter/issues/108))
* Removed `isEvaluationReady` & `isLoaded` flags as it had no use anyway.
* Added `controlsVisibleAtStart` flag. (Fixes [#113](https://github.com/sarbagyastha/youtube_player_flutter/issues/113))

## 6.0.1
* **(New Feature)** Added `title` and `author` property to `YoutubePlayerController`.
* Removed **DataConnectionChecker** dependency.
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ print(videoId); // BBAyRBTfsOU

[Detailed Example](https://github.com/sarbagyastha/youtube_player_flutter/tree/master/example)

## Note
Know more about the configuration options [here](https://pub.dartlang.org/documentation/youtube_player_flutter/latest/youtube_player_flutter/youtube_player_flutter-library.html).
## Quick Links
* [YoutubePlayer](https://pub.dev/documentation/youtube_player_flutter/latest/youtube_player_flutter/YoutubePlayer-class.html)
* [YoutubePlayerController](https://pub.dev/documentation/youtube_player_flutter/latest/youtube_player_flutter/YoutubePlayerController-class.html)
* [YoutubePlayerFlags](https://pub.dev/documentation/youtube_player_flutter/latest/youtube_player_flutter/YoutubePlayerFlags-class.html)
* [YoutubePlayerValue](https://pub.dev/documentation/youtube_player_flutter/latest/youtube_player_flutter/YoutubePlayerValue-class.html)


## Download
Download apk from above(in badges) and try the plugin.
Expand Down
33 changes: 22 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,30 @@ class _MyHomePageState extends State<MyHomePage> {
bool _muted = false;
bool _isPlayerReady = false;

final List<String> _ids = [
'gQDByCdjUXw',
'iLnmTe5Q2Qw',
'_WoCV4c6XOE',
'KmzdUe0RSJo',
'6jZDSSZZxjQ',
'p2lYr3vM_1w',
'7QUtEmBT_-w',
'34_PXCzGw1M',
];
int count = 0;

@override
void initState() {
super.initState();
_controller = YoutubePlayerController(
initialVideoId: 'p2lYr3vM_1w',
initialVideoId: 'QbjA8EiZJPk',
flags: YoutubePlayerFlags(
mute: false,
autoPlay: true,
forceHideAnnotation: true,
disableDragSeek: false,
loop: true,
loop: false,
isLive: false,
controlsVisibleAtStart: true,
),
)..addListener(listener);
_idController = TextEditingController();
Expand All @@ -80,9 +92,6 @@ class _MyHomePageState extends State<MyHomePage> {

void listener() {
if (_isPlayerReady) {
if (_controller.value.playerState == PlayerState.ended) {
_showSnackBar('Video Ended!');
}
if (mounted && !_controller.value.isFullScreen) {
setState(() {
_playerState = _controller.value.playerState;
Expand Down Expand Up @@ -167,6 +176,10 @@ class _MyHomePageState extends State<MyHomePage> {
onReady: () {
_isPlayerReady = true;
},
onEnded: (id) {
_controller.load(_ids[count++]);
_showSnackBar('Next Video Started!');
},
),
Padding(
padding: EdgeInsets.all(8.0),
Expand Down Expand Up @@ -335,11 +348,11 @@ class _MyHomePageState extends State<MyHomePage> {
Color _getStateColor(PlayerState state) {
switch (state) {
case PlayerState.unknown:
return Colors.redAccent;
case PlayerState.unStarted:
return Colors.grey[700];
case PlayerState.ended:
case PlayerState.unStarted:
return Colors.pink;
case PlayerState.ended:
return Colors.red;
case PlayerState.playing:
return Colors.blueAccent;
case PlayerState.paused:
Expand All @@ -348,8 +361,6 @@ class _MyHomePageState extends State<MyHomePage> {
return Colors.yellow;
case PlayerState.cued:
return Colors.blue[900];
case PlayerState.stopped:
return Colors.red;
default:
return Colors.blue;
}
Expand Down
7 changes: 1 addition & 6 deletions lib/src/enums/player_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ enum PlayerState {
/// Denotes state when player loads first video.
unStarted,

/// Denotes state when player ends playing a video.
///
/// Fires only once after video ends and switches to [PlayerState.stopped].
/// Denotes state when player has ended playing a video.
ended,

/// Denotes state when player is playing video.
Expand All @@ -28,7 +26,4 @@ enum PlayerState {

/// Denotes state when player loads video and is ready to be played.
cued,

/// Denotes state when player has stopped playing videos.
stopped,
}
7 changes: 7 additions & 0 deletions lib/src/player/fullscreen_youtube_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Future<void> showFullScreenYoutubePlayer({
Duration controlsTimeOut,
Color liveUIColor,
VoidCallback onReady,
void Function(String) onEnded,
ProgressBarColors progressColors,
String thumbnailUrl,
}) async =>
Expand All @@ -36,6 +37,7 @@ Future<void> showFullScreenYoutubePlayer({
controlsTimeOut: controlsTimeOut,
liveUIColor: liveUIColor,
onReady: onReady,
onEnded: onEnded,
progressColors: progressColors,
thumbnailUrl: thumbnailUrl,
),
Expand All @@ -58,6 +60,9 @@ class _FullScreenYoutubePlayer extends StatefulWidget {
/// {@macro youtube_player_flutter.onReady}
final VoidCallback onReady;

/// {@macro youtube_player_flutter.onEnded}
final void Function(String) onEnded;

/// {@macro youtube_player_flutter.liveUIColor}
final Color liveUIColor;

Expand All @@ -80,6 +85,7 @@ class _FullScreenYoutubePlayer extends StatefulWidget {
this.bufferIndicator,
this.progressColors,
this.onReady,
this.onEnded,
this.liveUIColor = Colors.red,
this.topActions,
this.bottomActions,
Expand All @@ -104,6 +110,7 @@ class _FullScreenYoutubePlayerState extends State<_FullScreenYoutubePlayer> {
controlsTimeOut: widget.controlsTimeOut,
liveUIColor: widget.liveUIColor,
onReady: widget.onReady,
onEnded: widget.onEnded,
progressColors: widget.progressColors,
thumbnailUrl: widget.thumbnailUrl,
topActions: widget.topActions,
Expand Down
24 changes: 19 additions & 5 deletions lib/src/player/raw_youtube_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import '../utils/youtube_player_controller.dart';
///
/// Use [YoutubePlayer] instead.
class RawYoutubePlayer extends StatefulWidget {
/// {@macro youtube_player_flutter.onEnded}
final void Function(String videoId) onEnded;

/// Creates a [RawYoutubePlayer] widget.
RawYoutubePlayer({Key key}) : super(key: key);
RawYoutubePlayer({
Key key,
this.onEnded,
}) : super(key: key);

@override
_RawYoutubePlayerState createState() => _RawYoutubePlayerState();
Expand Down Expand Up @@ -92,8 +98,13 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
);
break;
case '0':
if (widget.onEnded != null) {
widget.onEnded(controller.value.videoId);
}
controller.updateValue(
controller.value.copyWith(playerState: PlayerState.ended),
controller.value.copyWith(
playerState: PlayerState.ended,
),
);
break;
case '1':
Expand All @@ -116,13 +127,16 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
break;
case '3':
controller.updateValue(
controller.value
.copyWith(playerState: PlayerState.buffering),
controller.value.copyWith(
playerState: PlayerState.buffering,
),
);
break;
case '5':
controller.updateValue(
controller.value.copyWith(playerState: PlayerState.cued),
controller.value.copyWith(
playerState: PlayerState.cued,
),
);
break;
default:
Expand Down
44 changes: 27 additions & 17 deletions lib/src/player/youtube_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../enums/player_state.dart';
import '../enums/thumbnail_quality.dart';
import '../utils/errors.dart';
import '../utils/youtube_player_controller.dart';
Expand Down Expand Up @@ -88,6 +87,13 @@ class YoutubePlayer extends StatefulWidget {
/// {@endtemplate}
final VoidCallback onReady;

/// {@template youtube_player_flutter.onEnded}
/// Called when player had ended playing a video.
///
/// Returns video id that has ended playing.
/// {@endtemplate}
final void Function(String videoId) onEnded;

/// {@template youtube_player_flutter.liveUIColor}
/// Overrides color of Live UI when enabled.
/// {@endtemplate}
Expand Down Expand Up @@ -135,6 +141,7 @@ class YoutubePlayer extends StatefulWidget {
this.progressIndicatorColor = Colors.red,
this.progressColors,
this.onReady,
this.onEnded,
this.liveUIColor = Colors.red,
this.topActions,
this.bottomActions,
Expand Down Expand Up @@ -201,17 +208,19 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
if (controller.value.isReady && _initialLoad) {
_initialLoad = false;
if (controller.flags.autoPlay) controller.play();
controller.updateValue(
controller.value.copyWith(videoId: controller.initialVideoId),
);
if (controller.flags.mute) controller.mute();
if (widget.onReady != null) widget.onReady();
if (controller.flags.controlsVisibleAtStart) {
controller.updateValue(
controller.value.copyWith(isControlsVisible: true),
);
}
}
if (controller.value.toggleFullScreen) {
controller.updateValue(
controller.value.copyWith(
toggleFullScreen: false,
showControls: false,
isControlsVisible: false,
),
);
if (controller.value.isFullScreen) {
Expand Down Expand Up @@ -247,14 +256,6 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
Future.delayed(Duration(seconds: 2), () => controller.play());
}
}
if (controller.value.playerState == PlayerState.ended) {
controller.updateValue(
controller.value.copyWith(playerState: PlayerState.stopped),
);
if (controller.flags.loop) {
controller.load(controller.value.videoId);
}
}
if (mounted) setState(() {});
}

Expand Down Expand Up @@ -329,7 +330,16 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
fit: StackFit.expand,
overflow: Overflow.visible,
children: [
RawYoutubePlayer(),
RawYoutubePlayer(
onEnded: (String id) {
if (controller.flags.loop) {
controller.load(controller.value.videoId);
}
if (widget.onEnded != null) {
widget.onEnded(id);
}
},
),
if (!controller.flags.hideThumbnail)
AnimatedOpacity(
opacity: controller.value.hasPlayed ? 0 : 1,
Expand All @@ -350,7 +360,7 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
),
if (!controller.flags.hideControls &&
controller.value.position > Duration(milliseconds: 100) &&
!controller.value.showControls &&
!controller.value.isControlsVisible &&
widget.showVideoProgressIndicator &&
!controller.flags.isLive &&
!controller.value.isFullScreen)
Expand Down Expand Up @@ -380,7 +390,7 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
right: 0,
child: AnimatedOpacity(
opacity: !controller.flags.hideControls &&
controller.value.showControls
controller.value.isControlsVisible
? 1
: 0,
duration: Duration(milliseconds: 300),
Expand Down Expand Up @@ -411,7 +421,7 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
right: 0,
child: AnimatedOpacity(
opacity: !controller.flags.hideControls &&
controller.value.showControls
controller.value.isControlsVisible
? 1
: 0,
duration: Duration(milliseconds: 300),
Expand Down
22 changes: 8 additions & 14 deletions lib/src/utils/youtube_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class YoutubePlayerValue {
/// of a [YoutubePlayerController].
YoutubePlayerValue({
this.isReady = false,
this.showControls = false,
this.isLoaded = false,
this.isControlsVisible = false,
this.hasPlayed = false,
this.duration = const Duration(),
this.position = const Duration(),
Expand All @@ -40,11 +39,8 @@ class YoutubePlayerValue {
/// Returns true when the player is ready to play videos.
final bool isReady;

/// Whether to show controls or not.
final bool showControls;

/// Returns true once video loads.
final bool isLoaded;
/// Defines whether or not the controls are visible.
final bool isControlsVisible;

/// Returns true once the video start playing for the first time.
final bool hasPlayed;
Expand All @@ -55,7 +51,7 @@ class YoutubePlayerValue {
/// The current position of the video.
final Duration position;

/// The position up to which the video is buffered.
/// The position up to which the video is buffered.i
final double buffered;

/// Reports true if video is playing.
Expand Down Expand Up @@ -107,7 +103,7 @@ class YoutubePlayerValue {
/// the old one.
YoutubePlayerValue copyWith({
bool isReady,
bool showControls,
bool isControlsVisible,
bool isLoaded,
bool hasPlayed,
Duration duration,
Expand All @@ -129,8 +125,7 @@ class YoutubePlayerValue {
}) {
return YoutubePlayerValue(
isReady: isReady ?? this.isReady,
showControls: showControls ?? this.showControls,
isLoaded: isLoaded ?? this.isLoaded,
isControlsVisible: isControlsVisible ?? this.isControlsVisible,
duration: duration ?? this.duration,
hasPlayed: hasPlayed ?? this.hasPlayed,
position: position ?? this.position,
Expand Down Expand Up @@ -158,8 +153,7 @@ class YoutubePlayerValue {
'title: $title, '
'author: $author, '
'isReady: $isReady, '
'showControls: $showControls, '
'isLoaded: $isLoaded, '
'isControlsVisible: $isControlsVisible, '
'duration: $duration, '
'position: $position, '
'buffered: $buffered, '
Expand Down Expand Up @@ -303,7 +297,7 @@ class YoutubePlayerController extends ValueNotifier<YoutubePlayerValue> {
value.copyWith(
isReady: false,
isFullScreen: false,
showControls: false,
isControlsVisible: false,
playerState: PlayerState.unknown,
hasPlayed: false,
duration: Duration(),
Expand Down
Loading

0 comments on commit f9806b8

Please sign in to comment.