Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add video controls to notification center #1160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/lib/pages/notification_player_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class _NotificationPlayerPageState extends State<NotificationPlayerPage> {
title: "Elephant dream",
author: "Some author",
imageUrl: Constants.catImageUrl,
skipForwardTimeInMilliseconds: 15000,
skipBackwardTimeInMilliseconds: 15000,
),
);
_betterPlayerController.setupDataSource(dataSource);
Expand Down
27 changes: 23 additions & 4 deletions ios/Classes/BetterPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ - (void) setupRemoteNotification :(BetterPlayer*) player{
NSString* title = dataSource[@"title"];
NSString* author = dataSource[@"author"];
NSString* imageUrl = dataSource[@"imageUrl"];
int64_t skipForwardTimeInMilliseconds = [dataSource[@"skipForwardTimeInMilliseconds"] intValue];
int64_t skipBackwardTimeInMilliseconds = [dataSource[@"skipBackwardTimeInMilliseconds"] intValue];

if (showNotification){
[self setRemoteCommandsNotificationActive];
[self setupRemoteCommands: player];
[self setupRemoteCommands: player, skipForwardTimeInMilliseconds, skipBackwardTimeInMilliseconds];
[self setupRemoteCommandNotification: player, title, author, imageUrl];
[self setupUpdateListener: player, title, author, imageUrl];
}
Expand All @@ -120,20 +122,23 @@ - (void) setRemoteCommandsNotificationNotActive{
}


- (void) setupRemoteCommands:(BetterPlayer*)player {
- (void) setupRemoteCommands:(BetterPlayer*)player, skipForwardTimeInMilliseconds, skipBackwardTimeInMilliseconds {
if (_remoteCommandsInitialized){
return;
}
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.togglePlayPauseCommand setEnabled:YES];
[commandCenter.playCommand setEnabled:YES];
[commandCenter.pauseCommand setEnabled:YES];
[commandCenter.nextTrackCommand setEnabled:NO];
[commandCenter.previousTrackCommand setEnabled:NO];
[commandCenter.skipForwardCommand setEnabled:YES];
[commandCenter.skipBackwardCommand setEnabled:YES];
if (@available(iOS 9.1, *)) {
[commandCenter.changePlaybackPositionCommand setEnabled:YES];
}

commandCenter.skipForwardCommand.preferredIntervals = @[@(skipForwardTimeInMilliseconds/1000)];
commandCenter.skipBackwardCommand.preferredIntervals = @[@(skipBackwardTimeInMilliseconds/1000)];

[commandCenter.togglePlayPauseCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
if (_notificationPlayer != [NSNull null]){
if (_notificationPlayer.isPlaying){
Expand All @@ -159,7 +164,21 @@ - (void) setupRemoteCommands:(BetterPlayer*)player {
return MPRemoteCommandHandlerStatusSuccess;
}];

[commandCenter.skipForwardCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
if (_notificationPlayer != [NSNull null]){
int64_t millis = [_notificationPlayer position] + skipForwardTimeInMilliseconds;
_notificationPlayer.eventSink(@{@"event" : @"seek", @"position": @(millis)});
}
return MPRemoteCommandHandlerStatusSuccess;
}];

[commandCenter.skipBackwardCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
if (_notificationPlayer != [NSNull null]){
int64_t millis = [_notificationPlayer position] - skipBackwardTimeInMilliseconds;
_notificationPlayer.eventSink(@{@"event" : @"seek", @"position": @(millis)});
}
return MPRemoteCommandHandlerStatusSuccess;
}];

if (@available(iOS 9.1, *)) {
[commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class BetterPlayerNotificationConfiguration {
///Image of the video, used in controls notification
final String? imageUrl;

///Time for skip forward, used in controls notification
final int? skipForwardTimeInMilliseconds;

///Time for skip backward, used in controls notification
final int? skipBackwardTimeInMilliseconds;

///Name of the notification channel. Used only in Android.
final String? notificationChannelName;

Expand All @@ -27,5 +33,7 @@ class BetterPlayerNotificationConfiguration {
this.imageUrl,
this.notificationChannelName,
this.activityName,
this.skipForwardTimeInMilliseconds = 10000,
this.skipBackwardTimeInMilliseconds = 10000,
});
}
12 changes: 12 additions & 0 deletions lib/src/core/better_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ class BetterPlayerController {
author: _betterPlayerDataSource?.notificationConfiguration?.author,
imageUrl:
_betterPlayerDataSource?.notificationConfiguration?.imageUrl,
skipForwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipBackwardTimeInMilliseconds,
notificationChannelName: _betterPlayerDataSource
?.notificationConfiguration?.notificationChannelName,
overriddenDuration: _betterPlayerDataSource!.overriddenDuration,
Expand Down Expand Up @@ -490,6 +494,10 @@ class BetterPlayerController {
author: _betterPlayerDataSource?.notificationConfiguration?.author,
imageUrl:
_betterPlayerDataSource?.notificationConfiguration?.imageUrl,
skipForwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipBackwardTimeInMilliseconds,
notificationChannelName: _betterPlayerDataSource
?.notificationConfiguration?.notificationChannelName,
overriddenDuration: _betterPlayerDataSource!.overriddenDuration,
Expand All @@ -510,6 +518,10 @@ class BetterPlayerController {
_betterPlayerDataSource?.notificationConfiguration?.author,
imageUrl:
_betterPlayerDataSource?.notificationConfiguration?.imageUrl,
skipForwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: _betterPlayerDataSource
?.notificationConfiguration?.skipBackwardTimeInMilliseconds,
notificationChannelName: _betterPlayerDataSource
?.notificationConfiguration?.notificationChannelName,
overriddenDuration: _betterPlayerDataSource!.overriddenDuration,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/video_player/method_channel_video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class MethodChannelVideoPlayer extends VideoPlayerPlatform {
'title': dataSource.title,
'author': dataSource.author,
'imageUrl': dataSource.imageUrl,
'skipForwardTimeInMilliseconds':
dataSource.skipForwardTimeInMilliseconds,
'skipBackwardTimeInMilliseconds':
dataSource.skipBackwardTimeInMilliseconds,
'notificationChannelName': dataSource.notificationChannelName,
'overriddenDuration': dataSource.overriddenDuration?.inMilliseconds,
'activityName': dataSource.activityName
Expand All @@ -87,6 +91,10 @@ class MethodChannelVideoPlayer extends VideoPlayerPlatform {
'title': dataSource.title,
'author': dataSource.author,
'imageUrl': dataSource.imageUrl,
'skipForwardTimeInMilliseconds':
dataSource.skipForwardTimeInMilliseconds,
'skipBackwardTimeInMilliseconds':
dataSource.skipBackwardTimeInMilliseconds,
'notificationChannelName': dataSource.notificationChannelName,
'overriddenDuration': dataSource.overriddenDuration?.inMilliseconds,
'licenseUrl': dataSource.licenseUrl,
Expand All @@ -108,6 +116,10 @@ class MethodChannelVideoPlayer extends VideoPlayerPlatform {
'title': dataSource.title,
'author': dataSource.author,
'imageUrl': dataSource.imageUrl,
'skipForwardTimeInMilliseconds':
dataSource.skipForwardTimeInMilliseconds,
'skipBackwardTimeInMilliseconds':
dataSource.skipBackwardTimeInMilliseconds,
'notificationChannelName': dataSource.notificationChannelName,
'overriddenDuration': dataSource.overriddenDuration?.inMilliseconds,
'activityName': dataSource.activityName,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/video_player/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
String? title,
String? author,
String? imageUrl,
int? skipForwardTimeInMilliseconds,
int? skipBackwardTimeInMilliseconds,
String? notificationChannelName,
Duration? overriddenDuration,
String? activityName,
Expand All @@ -300,6 +302,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
title: title,
author: author,
imageUrl: imageUrl,
skipForwardTimeInMilliseconds: skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: skipBackwardTimeInMilliseconds,
notificationChannelName: notificationChannelName,
overriddenDuration: overriddenDuration,
activityName: activityName,
Expand Down Expand Up @@ -327,6 +331,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
String? title,
String? author,
String? imageUrl,
int? skipForwardTimeInMilliseconds,
int? skipBackwardTimeInMilliseconds,
String? notificationChannelName,
Duration? overriddenDuration,
String? licenseUrl,
Expand All @@ -350,6 +356,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
title: title,
author: author,
imageUrl: imageUrl,
skipForwardTimeInMilliseconds: skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: skipBackwardTimeInMilliseconds,
notificationChannelName: notificationChannelName,
overriddenDuration: overriddenDuration,
licenseUrl: licenseUrl,
Expand All @@ -371,6 +379,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
String? title,
String? author,
String? imageUrl,
int? skipForwardTimeInMilliseconds,
int? skipBackwardTimeInMilliseconds,
String? notificationChannelName,
Duration? overriddenDuration,
String? activityName,
Expand All @@ -383,6 +393,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
title: title,
author: author,
imageUrl: imageUrl,
skipForwardTimeInMilliseconds: skipForwardTimeInMilliseconds,
skipBackwardTimeInMilliseconds: skipBackwardTimeInMilliseconds,
notificationChannelName: notificationChannelName,
overriddenDuration: overriddenDuration,
activityName: activityName,
Expand Down
6 changes: 6 additions & 0 deletions lib/src/video_player/video_player_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class DataSource {
this.title,
this.author,
this.imageUrl,
this.skipForwardTimeInMilliseconds,
this.skipBackwardTimeInMilliseconds,
this.notificationChannelName,
this.overriddenDuration,
this.licenseUrl,
Expand Down Expand Up @@ -289,6 +291,10 @@ class DataSource {

final String? imageUrl;

final int? skipForwardTimeInMilliseconds;

final int? skipBackwardTimeInMilliseconds;

final String? notificationChannelName;

final Duration? overriddenDuration;
Expand Down
2 changes: 2 additions & 0 deletions test/mock_video_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class MockVideoPlayerController extends VideoPlayerController {
String? title,
String? author,
String? imageUrl,
int? skipForwardTimeInMilliseconds,
int? skipBackwardTimeInMilliseconds,
String? notificationChannelName,
Duration? overriddenDuration,
String? licenseUrl,
Expand Down