Skip to content

Commit

Permalink
Minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
sarbagyastha committed Nov 14, 2019
1 parent 8badd42 commit a064b17
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
13 changes: 8 additions & 5 deletions lib/src/player/raw_youtube_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
Completer<WebViewController>();
YoutubePlayerController controller;
PlayerState _cachedPlayerState;
bool _isPlayerReady = false;

@override
void initState() {
Expand All @@ -55,10 +56,10 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
case AppLifecycleState.suspending:
_cachedPlayerState = controller.value.playerState;
controller?.pause();
break;
default:
}
}

Expand All @@ -75,7 +76,7 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
JavascriptChannel(
name: 'Ready',
onMessageReceived: (JavascriptMessage message) {
controller.updateValue(controller.value.copyWith(isReady: true));
_isPlayerReady = true;
},
),
JavascriptChannel(
Expand Down Expand Up @@ -205,9 +206,11 @@ class _RawYoutubePlayerState extends State<RawYoutubePlayer>
);
},
onPageFinished: (_) {
controller.updateValue(
controller.value.copyWith(isEvaluationReady: true),
);
if (_isPlayerReady) {
controller.updateValue(
controller.value.copyWith(isReady: true),
);
}
},
),
);
Expand Down
12 changes: 2 additions & 10 deletions lib/src/player/youtube_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
}

void listener() async {
if (controller.value.isReady &&
controller.value.isEvaluationReady &&
_initialLoad) {
if (controller.value.isReady && _initialLoad) {
_initialLoad = false;
if (controller.flags.autoPlay) controller.play();
controller.updateValue(
Expand Down Expand Up @@ -332,11 +330,6 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
overflow: Overflow.visible,
children: [
RawYoutubePlayer(),
if (!controller.value.hasPlayed &&
controller.value.playerState == PlayerState.buffering)
Container(
color: Colors.black,
),
if (!controller.flags.hideThumbnail)
AnimatedOpacity(
opacity: controller.value.hasPlayed ? 0 : 1,
Expand Down Expand Up @@ -431,8 +424,7 @@ class _YoutubePlayerState extends State<YoutubePlayer> {
),
),
],
if (!controller.flags.hideControls &&
controller.value.isEvaluationReady)
if (!controller.flags.hideControls)
Center(
child: PlayPauseButton(),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/utils/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ String errorString(int errorCode, {String videoId = ''}) {
return 'The video requested was not found.';
case 101:
return 'The owner of the requested video does not allow it to be played.';
case 400:
return 'No Connection';
case 150:
return 'The owner of the requested video may not allow it to be played.';
default:
return 'Unknown Error';
}
Expand Down
26 changes: 13 additions & 13 deletions lib/src/utils/youtube_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class YoutubePlayerValue {
/// of a [YoutubePlayerController].
YoutubePlayerValue({
this.isReady = false,
this.isEvaluationReady = false,
this.showControls = false,
this.isLoaded = false,
this.hasPlayed = false,
Expand All @@ -38,12 +37,9 @@ class YoutubePlayerValue {
this.author = '',
});

/// Returns true when underlying web player reports ready.
/// Returns true when the player is ready to play videos.
final bool isReady;

/// Returns true when JavaScript evaluation can be triggered.
final bool isEvaluationReady;

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

Expand Down Expand Up @@ -111,7 +107,6 @@ class YoutubePlayerValue {
/// the old one.
YoutubePlayerValue copyWith({
bool isReady,
bool isEvaluationReady,
bool showControls,
bool isLoaded,
bool hasPlayed,
Expand All @@ -134,7 +129,6 @@ class YoutubePlayerValue {
}) {
return YoutubePlayerValue(
isReady: isReady ?? this.isReady,
isEvaluationReady: isEvaluationReady ?? this.isEvaluationReady,
showControls: showControls ?? this.showControls,
isLoaded: isLoaded ?? this.isLoaded,
duration: duration ?? this.duration,
Expand Down Expand Up @@ -164,7 +158,6 @@ class YoutubePlayerValue {
'title: $title, '
'author: $author, '
'isReady: $isReady, '
'isEvaluationReady: $isEvaluationReady, '
'showControls: $showControls, '
'isLoaded: $isLoaded, '
'duration: $duration, '
Expand Down Expand Up @@ -200,7 +193,7 @@ class YoutubePlayerController extends ValueNotifier<YoutubePlayerValue> {
this.flags = const YoutubePlayerFlags(),
}) : assert(initialVideoId != null, 'initialVideoId can\'t be null.'),
assert(flags != null),
super(YoutubePlayerValue(isReady: false));
super(YoutubePlayerValue());

/// Finds [YoutubePlayerController] in the provided context.
static YoutubePlayerController of(BuildContext context) {
Expand All @@ -210,7 +203,7 @@ class YoutubePlayerController extends ValueNotifier<YoutubePlayerValue> {
}

_callMethod(String methodString) {
if (value.isEvaluationReady) {
if (value.isReady) {
value.webViewController?.evaluateJavascript(methodString);
} else {
print('The controller is not ready for method calls.');
Expand All @@ -230,13 +223,21 @@ class YoutubePlayerController extends ValueNotifier<YoutubePlayerValue> {
/// Loads the video as per the [videoId] provided.
void load(String videoId, {int startAt = 0}) {
_updateValues(videoId);
_callMethod('loadById("$videoId",$startAt)');
if (value.errorCode == 1) {
pause();
} else {
_callMethod('loadById("$videoId",$startAt)');
}
}

/// Cues the video as per the [videoId] provided.
void cue(String videoId, {int startAt = 0}) {
_updateValues(videoId);
_callMethod('cueById("$videoId",$startAt)');
if (value.errorCode == 1) {
pause();
} else {
_callMethod('cueById("$videoId",$startAt)');
}
}

void _updateValues(String id) {
Expand Down Expand Up @@ -301,7 +302,6 @@ class YoutubePlayerController extends ValueNotifier<YoutubePlayerValue> {
void reset() => updateValue(
value.copyWith(
isReady: false,
isEvaluationReady: false,
isFullScreen: false,
showControls: false,
playerState: PlayerState.unknown,
Expand Down

0 comments on commit a064b17

Please sign in to comment.