Skip to content

Commit

Permalink
Merge pull request #228 from manikandn/issue_88
Browse files Browse the repository at this point in the history
Show replay button if video completes for Android OS
  • Loading branch information
Ahmadre authored Nov 12, 2020
2 parents 0ba49f8 + 6d99504 commit 1a5f48e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
55 changes: 54 additions & 1 deletion lib/src/cupertino_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ class CupertinoControls extends StatefulWidget {
}
}

class _CupertinoControlsState extends State<CupertinoControls> {
class _CupertinoControlsState extends State<CupertinoControls> with SingleTickerProviderStateMixin {
VideoPlayerValue _latestValue;
double _latestVolume;
bool _hideStuff = true;
Timer _hideTimer;
final marginSize = 5.0;
Timer _expandCollapseTimer;
Timer _initTimer;
bool _dragging = false;

VideoPlayerController controller;
ChewieController chewieController;
AnimationController playPauseIconAnimationController;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -107,6 +109,14 @@ class _CupertinoControlsState extends State<CupertinoControls> {
chewieController = ChewieController.of(context);
controller = chewieController.videoPlayerController;

if(playPauseIconAnimationController == null) {
playPauseIconAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 400),
reverseDuration: Duration(milliseconds: 400),
);
}

if (_oldController != chewieController) {
_dispose();
_initialize();
Expand Down Expand Up @@ -215,6 +225,8 @@ class _CupertinoControlsState extends State<CupertinoControls> {
}

Expanded _buildHitArea() {
bool isFinished = _latestValue.position >= _latestValue.duration;

return Expanded(
child: GestureDetector(
onTap: _latestValue != null && _latestValue.isPlaying
Expand All @@ -228,6 +240,39 @@ class _CupertinoControlsState extends State<CupertinoControls> {
},
child: Container(
color: Colors.transparent,
child: Center(
child: AnimatedOpacity(
opacity:
_latestValue != null && !_latestValue.isPlaying && !_dragging
? 1.0
: 0.0,
duration: Duration(milliseconds: 300),
child: GestureDetector(
child: Container(
decoration: BoxDecoration(
color: widget.backgroundColor,
borderRadius: BorderRadius.circular(48.0),
),
child: Padding(
padding: EdgeInsets.all(12.0),
child: IconButton(
icon: isFinished
? Icon(Icons.replay, size: 32.0, color: widget.iconColor)
: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: playPauseIconAnimationController,
size: 32.0,
color: widget.iconColor
),
onPressed: (){
_playPause();
}
),
),
),
),
),
),
),
),
);
Expand Down Expand Up @@ -505,9 +550,17 @@ class _CupertinoControlsState extends State<CupertinoControls> {
child: CupertinoVideoProgressBar(
controller,
onDragStart: () {
setState(() {
_dragging = true;
});

_hideTimer?.cancel();
},
onDragEnd: () {
setState(() {
_dragging = false;
});

_startHideTimer();
},
colors: chewieController.cupertinoProgressColors ??
Expand Down
29 changes: 27 additions & 2 deletions lib/src/material_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MaterialControls extends StatefulWidget {
}
}

class _MaterialControlsState extends State<MaterialControls> {
class _MaterialControlsState extends State<MaterialControls> with SingleTickerProviderStateMixin{
VideoPlayerValue _latestValue;
double _latestVolume;
bool _hideStuff = true;
Expand All @@ -31,6 +31,7 @@ class _MaterialControlsState extends State<MaterialControls> {

VideoPlayerController controller;
ChewieController chewieController;
AnimationController playPauseIconAnimationController;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -96,6 +97,14 @@ class _MaterialControlsState extends State<MaterialControls> {
chewieController = ChewieController.of(context);
controller = chewieController.videoPlayerController;

if(playPauseIconAnimationController == null) {
playPauseIconAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 400),
reverseDuration: Duration(milliseconds: 400),
);
}

if (_oldController != chewieController) {
_dispose();
_initialize();
Expand Down Expand Up @@ -163,6 +172,8 @@ class _MaterialControlsState extends State<MaterialControls> {
}

Expanded _buildHitArea() {
bool isFinished = _latestValue.position >= _latestValue.duration;

return Expanded(
child: GestureDetector(
onTap: () {
Expand Down Expand Up @@ -198,7 +209,18 @@ class _MaterialControlsState extends State<MaterialControls> {
),
child: Padding(
padding: EdgeInsets.all(12.0),
child: Icon(Icons.play_arrow, size: 32.0),
child: IconButton(
icon: isFinished
? Icon(Icons.replay, size: 32.0)
: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: playPauseIconAnimationController,
size: 32.0,
),
onPressed: (){
_playPause();
}
),
),
),
),
Expand Down Expand Up @@ -379,6 +401,7 @@ class _MaterialControlsState extends State<MaterialControls> {

setState(() {
if (controller.value.isPlaying) {
playPauseIconAnimationController.reverse();
_hideStuff = false;
_hideTimer?.cancel();
controller.pause();
Expand All @@ -388,11 +411,13 @@ class _MaterialControlsState extends State<MaterialControls> {
if (!controller.value.initialized) {
controller.initialize().then((_) {
controller.play();
playPauseIconAnimationController.forward();
});
} else {
if (isFinished) {
controller.seekTo(Duration(seconds: 0));
}
playPauseIconAnimationController.forward();
controller.play();
}
}
Expand Down

0 comments on commit 1a5f48e

Please sign in to comment.