Skip to content

Commit

Permalink
Feature/february changes 7 (#315)
Browse files Browse the repository at this point in the history
* Fixed empty data source notification issue

* Fixed empty data source notification issue

* Fixed WebVTT subtitles parsing issue.

* Fixed WebVTT subtitles parsing issue.

* Fixed example subtitles

* Fixed memory data source issue on iOS

* Fixed controls visible all time on live stream

* Updated data sources

* Fixed potential iOS notification crash.

* Format & DA fixes

Co-authored-by: jhomlala <j.homlala@gmail.com>
  • Loading branch information
jhomlala and jhomlala authored Feb 20, 2021
1 parent 0af5c33 commit 8ad1afe
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 59 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.0.56
* Fixed empty data source notification issue.
* Fixed WebVTT subtitles parsing issue.
* Fixed memory data source issue on iOS.
* Added videoExtension parameter for memory data source (works only with memory data source).
* Added videoFormat parameter to network data source.
* Fixed controls visible all time on live stream.
* Fixed potential iOS notification crash.

## 0.0.55
* Dart analysis fix

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This plugin is based on [Chewie](https://github.com/brianegan/chewie). Chewie is

```yaml
dependencies:
better_player: ^0.0.55
better_player: ^0.0.56
```
2. Install it
Expand Down Expand Up @@ -640,6 +640,9 @@ Possible configuration options:
///Video format hint when data source url has not valid extension.
final BetterPlayerVideoFormat videoFormat;
///Extension of video without dot. Used only in memory data source.
final String videoExtension;
```


Expand Down
2 changes: 0 additions & 2 deletions example/assets/example_subtitles.srt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
WEBVTT

1
00:00:00.000 --> 00:00:02.000
Welcome to Better Player
Expand Down
3 changes: 2 additions & 1 deletion example/lib/pages/memory_player_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class _MemoryPlayerPageState extends State<MemoryPlayerPage> {
File file = File(filePath);

List<int> bytes = file.readAsBytesSync().buffer.asUint8List();
BetterPlayerDataSource dataSource = BetterPlayerDataSource.memory(bytes);
BetterPlayerDataSource dataSource =
BetterPlayerDataSource.memory(bytes, videoExtension: "mp4");
_betterPlayerController.setupDataSource(dataSource);
}

Expand Down
50 changes: 29 additions & 21 deletions ios/Classes/FLTBetterPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -982,35 +982,43 @@ - (void) setupRemoteCommandNotification:(FLTBetterPlayer*)player, NSString* titl
NSString* key = [self getTextureId:player];
MPMediaItemArtwork* artworkImage = [_artworkImageDict objectForKey:key];

if (artworkImage){
[nowPlayingInfoDict setObject:artworkImage forKey:MPMediaItemPropertyArtwork];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfoDict;

} else {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
UIImage * tempArtworkImage = nil;
if ([imageUrl rangeOfString:@"http"].location == NSNotFound){
tempArtworkImage = [UIImage imageWithContentsOfFile:imageUrl];
} else {
NSURL *nsImageUrl =[NSURL URLWithString:imageUrl];
tempArtworkImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:nsImageUrl]];
}
if(tempArtworkImage)
{
MPMediaItemArtwork* artworkImage = [[MPMediaItemArtwork alloc] initWithImage: tempArtworkImage];
[_artworkImageDict setObject:artworkImage forKey:key];
[nowPlayingInfoDict setObject:artworkImage forKey:MPMediaItemPropertyArtwork];
}
if (key != [NSNull null]){
if (artworkImage){
[nowPlayingInfoDict setObject:artworkImage forKey:MPMediaItemPropertyArtwork];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfoDict;
});

} else {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
@try{
UIImage * tempArtworkImage = nil;
if ([imageUrl rangeOfString:@"http"].location == NSNotFound){
tempArtworkImage = [UIImage imageWithContentsOfFile:imageUrl];
} else {
NSURL *nsImageUrl =[NSURL URLWithString:imageUrl];
tempArtworkImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:nsImageUrl]];
}
if(tempArtworkImage)
{
MPMediaItemArtwork* artworkImage = [[MPMediaItemArtwork alloc] initWithImage: tempArtworkImage];
[_artworkImageDict setObject:artworkImage forKey:key];
[nowPlayingInfoDict setObject:artworkImage forKey:MPMediaItemPropertyArtwork];
}
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfoDict;
}
@catch(NSException *exception) {

}
});
}
}
} else {
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfoDict;
}
}



- (NSString*) getTextureId: (FLTBetterPlayer*) player{
NSArray* temp = [_players allKeysForObject: player];
NSString* key = [temp lastObject];
Expand Down
34 changes: 25 additions & 9 deletions lib/src/configuration/better_player_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Project imports:

import 'package:better_player/src/configuration/better_player_data_source_type.dart';
import 'package:better_player/src/configuration/better_player_notification_configuration.dart';
import 'package:better_player/src/configuration/better_player_video_format.dart';
Expand Down Expand Up @@ -58,6 +59,9 @@ class BetterPlayerDataSource {
///Video format hint when data source url has not valid extension.
final BetterPlayerVideoFormat videoFormat;

///Extension of video without dot. Used only in memory data source.
final String videoExtension;

BetterPlayerDataSource(
this.type,
this.url, {
Expand All @@ -75,6 +79,7 @@ class BetterPlayerDataSource {
const BetterPlayerNotificationConfiguration(showNotification: false),
this.overriddenDuration,
this.videoFormat,
this.videoExtension,
}) : assert(
((type == BetterPlayerDataSourceType.network ||
type == BetterPlayerDataSourceType.file) &&
Expand All @@ -97,6 +102,7 @@ class BetterPlayerDataSource {
BetterPlayerCacheConfiguration cacheConfiguration,
BetterPlayerNotificationConfiguration notificationConfiguration,
Duration overriddenDuration,
BetterPlayerVideoFormat videoFormat,
}) {
return BetterPlayerDataSource(
BetterPlayerDataSourceType.network,
Expand All @@ -111,6 +117,7 @@ class BetterPlayerDataSource {
cacheConfiguration: cacheConfiguration,
notificationConfiguration: notificationConfiguration,
overriddenDuration: overriddenDuration,
videoFormat: videoFormat,
);
}

Expand Down Expand Up @@ -140,6 +147,7 @@ class BetterPlayerDataSource {
///Url parameter is not used in this data source.
factory BetterPlayerDataSource.memory(
List<int> bytes, {
String videoExtension,
List<BetterPlayerSubtitlesSource> subtitles,
bool useHlsSubtitles,
bool useHlsTracks,
Expand All @@ -148,15 +156,19 @@ class BetterPlayerDataSource {
BetterPlayerNotificationConfiguration notificationConfiguration,
Duration overriddenDuration,
}) {
return BetterPlayerDataSource(BetterPlayerDataSourceType.memory, "",
bytes: bytes,
subtitles: subtitles,
useHlsSubtitles: useHlsSubtitles,
useHlsTracks: useHlsTracks,
resolutions: qualities,
cacheConfiguration: cacheConfiguration,
notificationConfiguration: notificationConfiguration,
overriddenDuration: overriddenDuration);
return BetterPlayerDataSource(
BetterPlayerDataSourceType.memory,
"",
videoExtension: videoExtension,
bytes: bytes,
subtitles: subtitles,
useHlsSubtitles: useHlsSubtitles,
useHlsTracks: useHlsTracks,
resolutions: qualities,
cacheConfiguration: cacheConfiguration,
notificationConfiguration: notificationConfiguration,
overriddenDuration: overriddenDuration,
);
}

BetterPlayerDataSource copyWith({
Expand All @@ -173,6 +185,8 @@ class BetterPlayerDataSource {
BetterPlayerCacheConfiguration cacheConfiguration,
BetterPlayerNotificationConfiguration notificationConfiguration,
Duration overriddenDuration,
BetterPlayerVideoFormat videoFormat,
String videoExtension,
}) {
return BetterPlayerDataSource(
type ?? this.type,
Expand All @@ -189,6 +203,8 @@ class BetterPlayerDataSource {
notificationConfiguration:
notificationConfiguration ?? this.notificationConfiguration,
overriddenDuration: overriddenDuration ?? this.overriddenDuration,
videoFormat: videoFormat ?? this.videoFormat,
videoExtension: videoExtension ?? this.videoExtension,
);
}
}
2 changes: 2 additions & 0 deletions lib/src/controls/better_player_controls_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ abstract class BetterPlayerControlsState<T extends StatefulWidget>
bool isVideoFinished(VideoPlayerValue videoPlayerValue) {
return videoPlayerValue?.position != null &&
videoPlayerValue?.duration != null &&
videoPlayerValue.position.inMilliseconds != 0 &&
videoPlayerValue.duration.inMilliseconds != 0 &&
videoPlayerValue.position >= videoPlayerValue.duration;
}

Expand Down
14 changes: 10 additions & 4 deletions lib/src/core/better_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class BetterPlayerController extends ChangeNotifier {

///Process data source
await _setupDataSource(betterPlayerDataSource);

notifyListeners();
}

///Configure subtitles based on subtitles source.
Expand Down Expand Up @@ -384,7 +386,8 @@ class BetterPlayerController extends ChangeNotifier {
);
break;
case BetterPlayerDataSourceType.memory:
final file = await _createFile(_betterPlayerDataSource.bytes);
final file = await _createFile(_betterPlayerDataSource.bytes,
extension: _betterPlayerDataSource.videoExtension);

if (file != null) {
await videoPlayerController.setFileDataSource(
Expand Down Expand Up @@ -414,9 +417,10 @@ class BetterPlayerController extends ChangeNotifier {

///Create file from provided list of bytes. File will be created in temporary
///directory.
Future<File> _createFile(List<int> bytes) async {
Future<File> _createFile(List<int> bytes, {String extension = "temp"}) async {
final String dir = (await getTemporaryDirectory()).path;
final File temp = File('$dir/better_player_${DateTime.now()}.temp');
final File temp = File(
'$dir/better_player_${DateTime.now().millisecondsSinceEpoch}.$extension');
await temp.writeAsBytes(bytes);
return temp;
}
Expand Down Expand Up @@ -707,7 +711,9 @@ class BetterPlayerController extends ChangeNotifier {
_postEvent(
BetterPlayerEvent(BetterPlayerEventType.changedPlayerVisibility));

if (!_betterPlayerDataSource.notificationConfiguration.showNotification &&
if (!(_betterPlayerDataSource
?.notificationConfiguration?.showNotification ==
true) &&
betterPlayerConfiguration.handleLifecycle) {
if (betterPlayerConfiguration.playerVisibilityChangedBehavior != null) {
betterPlayerConfiguration
Expand Down
8 changes: 4 additions & 4 deletions lib/src/core/better_player_with_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class _BetterPlayerWithControlsState extends State<BetterPlayerWithControls> {
}

void _onControllerChanged() {
if (!_initalized) {
setState(() {
setState(() {
if (!_initalized) {
_initalized = true;
});
}
}
});
}

@override
Expand Down
36 changes: 21 additions & 15 deletions lib/src/subtitles/better_player_subtitle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class BetterPlayerSubtitle {
this.type,
});

factory BetterPlayerSubtitle(String value) {
factory BetterPlayerSubtitle(String value, bool isWebVTT) {
try {
final scanner = value.split('\n');
if (scanner.length == 2) {
return _handle2LinesSubtitles(scanner);
}
if (scanner.length > 2) {
return _handle3LinesAndMoreSubtitles(scanner);
return _handle3LinesAndMoreSubtitles(scanner, isWebVTT);
}
return BetterPlayerSubtitle._();
} catch (exception) {
Expand All @@ -50,21 +50,27 @@ class BetterPlayerSubtitle {
}

static BetterPlayerSubtitle _handle3LinesAndMoreSubtitles(
List<String> scanner) {
List<String> scanner, bool isWebVTT) {
try {
if (scanner[0].isEmpty) {
scanner.removeAt(0);
if (isWebVTT) {
final timeSplit = scanner[0].split(timerSeparator);
final start = _stringToDuration(timeSplit[0]);
final end = _stringToDuration(timeSplit[1]);
final texts = scanner.sublist(1, scanner.length);
return BetterPlayerSubtitle._(
index: -1, start: start, end: end, texts: texts);
} else {
if (scanner[0].isEmpty) {
scanner.removeAt(0);
}
final index = int.tryParse(scanner[0]);
final timeSplit = scanner[1].split(timerSeparator);
final start = _stringToDuration(timeSplit[0]);
final end = _stringToDuration(timeSplit[1]);
final texts = scanner.sublist(2, scanner.length);
return BetterPlayerSubtitle._(
index: index, start: start, end: end, texts: texts);
}

final index = int.tryParse(scanner[0]);

final timeSplit = scanner[1].split(timerSeparator);
final start = _stringToDuration(timeSplit[0]);
final end = _stringToDuration(timeSplit[1]);
final texts = scanner.sublist(2, scanner.length);

return BetterPlayerSubtitle._(
index: index, start: start, end: end, texts: texts);
} catch (exception) {
BetterPlayerUtils.log("Failed to parse subtitle line: $scanner");
return BetterPlayerSubtitle._();
Expand Down
3 changes: 2 additions & 1 deletion lib/src/subtitles/better_player_subtitles_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ class BetterPlayerSubtitlesFactory {

final List<BetterPlayerSubtitle> subtitlesObj = [];

final bool isWebVTT = components.contains("WEBVTT");
for (final component in components) {
if (component.isEmpty) {
continue;
}
final subtitle = BetterPlayerSubtitle(component);
final subtitle = BetterPlayerSubtitle(component, isWebVTT);
if (subtitle != null &&
subtitle.start != null &&
subtitle.end != null &&
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: better_player
description: Advanced video player based on video_player and Chewie. It's solves many typical use cases and it's easy to run.
version: 0.0.55
version: 0.0.56
authors:
- Jakub Homlala <jhomlala@gmail.com>
homepage: https://github.com/jhomlala/betterplayer
Expand Down

0 comments on commit 8ad1afe

Please sign in to comment.