Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Added HTTP headers setting to video_player plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarikk26 committed Nov 28, 2018
1 parent c8ae4a7 commit 4f62d9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static class VideoPlayer {
EventChannel eventChannel,
TextureRegistry.SurfaceTextureEntry textureEntry,
String dataSource,
Map<String, String> httpHeaders,
Result result) {
this.eventChannel = eventChannel;
this.textureEntry = textureEntry;
Expand All @@ -83,13 +84,18 @@ private static class VideoPlayer {
if (uri.getScheme().equals("asset") || uri.getScheme().equals("file")) {
dataSourceFactory = new DefaultDataSourceFactory(context, "ExoPlayer");
} else {
dataSourceFactory =
DefaultHttpDataSourceFactory httpDataSourceFactory =
new DefaultHttpDataSourceFactory(
"ExoPlayer",
null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true);

dataSourceFactory = httpDataSourceFactory;
if(httpHeaders != null) {
httpDataSourceFactory.getDefaultRequestProperties().set(httpHeaders);
}
}

MediaSource mediaSource = buildMediaSource(uri, dataSourceFactory, context);
Expand Down Expand Up @@ -315,15 +321,19 @@ public void onMethodCall(MethodCall call, Result result) {
eventChannel,
handle,
"asset:///" + assetLookupKey,
null,
result);
videoPlayers.put(handle.id(), player);
} else {
@SuppressWarnings("unchecked")
Map<String, String> httpHeaders = call.argument("httpHeaders");
player =
new VideoPlayer(
registrar.context(),
eventChannel,
handle,
(String) call.argument("uri"),
httpHeaders,
result);
videoPlayers.put(handle.id(), player);
}
Expand Down
15 changes: 10 additions & 5 deletions packages/video_player/ios/Classes/VideoPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ @interface FLTVideoPlayer : NSObject <FlutterTexture, FlutterStreamHandler>
@property(nonatomic, readonly) bool isPlaying;
@property(nonatomic, readonly) bool isLooping;
@property(nonatomic, readonly) bool isInitialized;
- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater;
- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater
httpHeaders:(NSDictionary<NSString*, NSString*>*)headers;
- (void)play;
- (void)pause;
- (void)setIsLooping:(bool)isLooping;
Expand All @@ -52,17 +53,20 @@ - (void)updatePlayingState;
@implementation FLTVideoPlayer
- (instancetype)initWithAsset:(NSString*)asset frameUpdater:(FLTFrameUpdater*)frameUpdater {
NSString* path = [[NSBundle mainBundle] pathForResource:asset ofType:nil];
return [self initWithURL:[NSURL fileURLWithPath:path] frameUpdater:frameUpdater];
return [self initWithURL:[NSURL fileURLWithPath:path] frameUpdater:frameUpdater httpHeaders:nil];
}

- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater {
- (instancetype)initWithURL:(NSURL*)url frameUpdater:(FLTFrameUpdater*)frameUpdater
httpHeaders:(NSDictionary<NSString*, NSString*>*)headers{
self = [super init];
NSAssert(self, @"super init cannot be nil");
_isInitialized = false;
_isPlaying = false;
_disposed = false;

AVURLAsset* urlAsset = [AVURLAsset URLAssetWithURL:url options:@{@"AVURLAssetHTTPHeaderFieldsKey": headers}];

AVPlayerItem* item = [AVPlayerItem playerItemWithURL:url];
AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:urlAsset];
[item addObserver:self
forKeyPath:@"loadedTimeRanges"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
Expand Down Expand Up @@ -340,8 +344,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
player = [[FLTVideoPlayer alloc] initWithAsset:assetPath frameUpdater:frameUpdater];
} else {
dataSource = argsMap[@"uri"];
NSDictionary<NSString*, NSString*>* httpHeaders = argsMap[@"httpHeaders"];
player = [[FLTVideoPlayer alloc] initWithURL:[NSURL URLWithString:dataSource]
frameUpdater:frameUpdater];
frameUpdater:frameUpdater httpHeaders: httpHeaders];
}
int64_t textureId = [_registry registerTexture:player];
frameUpdater.textureId = textureId;
Expand Down
6 changes: 5 additions & 1 deletion packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
Completer<void> _creatingCompleter;
StreamSubscription<dynamic> _eventSubscription;
_VideoAppLifeCycleObserver _lifeCycleObserver;
Map<String, String> httpHeaders;

@visibleForTesting
int get textureId => _textureId;
Expand All @@ -200,7 +201,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
};
break;
case DataSourceType.network:
dataSourceDescription = <String, dynamic>{'uri': dataSource};
dataSourceDescription = <String, dynamic>{
'uri': dataSource,
'httpHeaders': httpHeaders
};
break;
case DataSourceType.file:
dataSourceDescription = <String, dynamic>{'uri': dataSource};
Expand Down
2 changes: 2 additions & 0 deletions packages/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class FakeController extends ValueNotifier<VideoPlayerValue>
@override
String get package => null;
@override
Map<String, String> httpHeaders;
@override
Future<Duration> get position async => value.position;

@override
Expand Down

0 comments on commit 4f62d9a

Please sign in to comment.