Skip to content

Commit 79f00b9

Browse files
authored
Merge pull request #83 from souly1/master
adding headers to video
2 parents d7e4387 + 0b618af commit 79f00b9

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ If set to true, currentTimeUpdated callback is possible.
101101

102102
Attribute to specify an event callback to execute when the time is updated.
103103

104+
- **headers - (Map<string, string>)** - *optional*
105+
106+
Set headers to add when loading a video from URL.
107+
108+
104109
## API
105110

106111
- **play()** - Start playing the video

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-videoplayer",
3-
"version": "3.0.9",
3+
"version": "3.0.10",
44
"main": "videoplayer",
55
"typings": "videoplayer.d.ts",
66
"description": "A NativeScript plugin that uses the native video players to play local and remote videos.",
@@ -17,7 +17,8 @@
1717
}
1818
},
1919
"scripts": {
20-
"precommit": "lint-staged"
20+
"precommit": "lint-staged",
21+
"prepare": "tsc"
2122
},
2223
"lint-staged": {
2324
"*.ts": ["prettier --write", "git add"]
@@ -62,6 +63,11 @@
6263
"name": "Ivo Georgiev",
6364
"email": "[email protected]",
6465
"url": "https://github.com/Ivshti"
66+
},
67+
{
68+
"name": "Ophir Stern",
69+
"email": "[email protected]",
70+
"url": "https://github.com/souly1"
6571
}
6672
],
6773
"author": {

videoplayer-common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ function onSrcPropertyChanged(view, oldValue, newValue) {
3636
}
3737
}
3838

39+
function onHeadersPropertyChanged(view, oldValue, newValue) {
40+
const video = view;
41+
42+
if (oldValue !== newValue) {
43+
if (video.src) {
44+
let src = video.src;
45+
onSrcPropertyChanged(view, null, null);
46+
onSrcPropertyChanged(view, null, src);
47+
}
48+
}
49+
}
50+
3951
export class Video extends View {
4052
public static finishedEvent: string = "finished";
4153
public static playbackReadyEvent: string = "playbackReady";
@@ -47,6 +59,7 @@ export class Video extends View {
4759
public android: any;
4860
public ios: any;
4961
public src: string; /// video source file
62+
public headers: Map<string, string>; /// headers to use
5063
public observeCurrentTime: boolean; // set to true if want to observe current time.
5164
public autoplay: boolean = false; /// set true for the video to start playing when ready
5265
public controls: boolean = true; /// set true to enable the media player's playback controls
@@ -61,6 +74,12 @@ export const srcProperty = new Property<Video, any>({
6174
});
6275
srcProperty.register(Video);
6376

77+
export const headersProperty = new Property<Video, any>({
78+
name: "headers",
79+
valueChanged: onHeadersPropertyChanged
80+
});
81+
headersProperty.register(Video);
82+
6483
export const videoSourceProperty = new Property<Video, any>({
6584
name: "videoSource"
6685
});

videoplayer.android.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class Video extends common.Video {
1919
private videoWidth;
2020
private videoHeight;
2121
private _src;
22+
private _headers: java.util.Map<string, string>;
2223
private playState;
2324
private mediaState;
2425
private textureSurface;
@@ -37,6 +38,7 @@ export class Video extends common.Video {
3738
this.videoHeight = 0;
3839

3940
this._src = null;
41+
this._headers = null;
4042

4143
this.playState = STATE_IDLE;
4244
this.mediaState = SURFACE_WAITING;
@@ -52,6 +54,10 @@ export class Video extends common.Video {
5254
return this.nativeView;
5355
}
5456

57+
[common.headersProperty.setNative](value) {
58+
this._setHeader(value ? value : null);
59+
}
60+
5561
[common.videoSourceProperty.setNative](value) {
5662
this._setNativeVideo(value ? value.android : null);
5763
}
@@ -336,7 +342,13 @@ export class Video extends common.Video {
336342
}
337343

338344
private _openVideo(): void {
339-
if (this._src === null || this.textureSurface === null) {
345+
if (
346+
this._src === null ||
347+
this.textureSurface === null ||
348+
(this._src !== null &&
349+
typeof this._src === "string" &&
350+
this._src.length === 0)
351+
) {
340352
// we have to protect In case something else calls this before we are ready
341353
// the Surface event will then call this when we are ready...
342354
return;
@@ -366,9 +378,18 @@ export class Video extends common.Video {
366378

367379
this._setupMediaPlayerListeners();
368380

369-
this.mediaPlayer.setDataSource(
370-
/* utils.ad.getApplicationContext(),*/ this._src
371-
);
381+
if (!this._headers || this._headers.size() === 0) {
382+
this.mediaPlayer.setDataSource(
383+
/* utils.ad.getApplicationContext(),*/ this._src
384+
);
385+
} else {
386+
let videoUri = android.net.Uri.parse(this._src);
387+
this.mediaPlayer.setDataSource(
388+
utils.ad.getApplicationContext(),
389+
videoUri,
390+
this._headers
391+
);
392+
}
372393
this.mediaPlayer.setSurface(this.textureSurface);
373394
this.mediaPlayer.setAudioStreamType(
374395
android.media.AudioManager.STREAM_MUSIC
@@ -387,6 +408,18 @@ export class Video extends common.Video {
387408
this._openVideo();
388409
}
389410

411+
public _setHeader(headers: Map<string, string>): void {
412+
if (headers && headers.size > 0) {
413+
this._headers = new java.util.HashMap();
414+
headers.forEach((value: string, key: string) => {
415+
this._headers.put(key, value);
416+
});
417+
}
418+
if (this._src) {
419+
this._openVideo();
420+
}
421+
}
422+
390423
public setNativeSource(nativePlayerSrc: string): void {
391424
this._src = nativePlayerSrc;
392425
this._openVideo();

videoplayer.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export declare class Video extends View {
33
android: any;
44
ios: any;
55
src: string; /// video source file
6+
headers: Map<string, string>; /// headers to use
67
loop: boolean; /// whether the video loops the playback after extends
78
autoplay: boolean; /// set true for the video to start playing when ready
89
controls: boolean; /// set true to enable the media player's playback controls

videoplayer.ios.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Video extends common.Video {
2525
private _player: any; /// AVPlayer
2626
private _playerController: any; /// AVPlayerViewController
2727
private _src: string;
28+
private _headers: NSMutableDictionary<string, string>;
2829
private _didPlayToEndTimeObserver: any;
2930
private _didPlayToEndTimeActive: boolean;
3031
private _observer: NSObject;
@@ -53,11 +54,43 @@ export class Video extends common.Video {
5354
return this.nativeView;
5455
}
5556

57+
[common.headersProperty.setNative](value) {
58+
this._setHeader(value ? value : null);
59+
}
60+
5661
[common.videoSourceProperty.setNative](value: AVPlayerItem) {
5762
this._setNativeVideo(value ? value.ios : null);
5863
}
5964

65+
public _setHeader(headers: Map<string, string>): void {
66+
if (headers && headers.size > 0) {
67+
this._headers = new NSMutableDictionary();
68+
headers.forEach((value: string, key: string) => {
69+
this._headers.setValueForKey(value, key);
70+
});
71+
}
72+
if (this._src) {
73+
this._setNativePlayerSource(this._src);
74+
}
75+
}
76+
6077
public _setNativeVideo(nativeVideoPlayer: any) {
78+
console.log("set native player source!");
79+
if (this["_url"] && this._headers && this._headers.count > 0) {
80+
// adding headers if exist when loading video from URL
81+
console.log("need to add headers!");
82+
let url: any = NSURL.URLWithString(this["_url"]);
83+
let options = NSDictionary.dictionaryWithDictionary({
84+
AVURLAssetHTTPHeaderFieldsKey: this._headers
85+
});
86+
let asset: AVURLAsset = AVURLAsset.alloc().initWithURLOptions(
87+
url,
88+
options
89+
);
90+
let item: AVPlayerItem = AVPlayerItem.playerItemWithAsset(asset);
91+
nativeVideoPlayer = item;
92+
}
93+
6194
if (nativeVideoPlayer != null) {
6295
let currentItem = this._player.currentItem;
6396
this._addStatusObserver(nativeVideoPlayer);

0 commit comments

Comments
 (0)