Skip to content

Commit ada44d4

Browse files
committed
2.1.2
1 parent 3d0450d commit ada44d4

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-videoplayer",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"main": "videoplayer.js",
55
"typings": "videoplayer.d.ts",
66
"description": "A NativeScript plugin that uses the native video players to play local and remote videos.",

video-source/video-source.ios.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import common = require("./video-source-common");
44
import enums = require("ui/enums");
55
import definition = require("./video-source");
66

7-
declare var android, AVPlayer, NSBundle, NSURL;
7+
declare var android, AVPlayerItem, NSBundle, NSURL;
88

99
global.moduleMerge(common, exports);
1010

1111
export class VideoSource implements definition.VideoSource {
1212
public android: any; /// android.widget.VideoView
13-
public ios: any; /// AVPlayer
13+
public ios: any; /// AVPlayerItem
1414
height: any;
1515
width: any;
1616

1717
public loadFromResource(name: string): boolean {
1818
let videoURL = NSBundle.mainBundle().URLForResourceWithExtension(name, null);
19-
let player = new AVPlayer(videoURL);
19+
let player = new AVPlayerItem(videoURL);
2020
this.ios = player;
2121
return this.ios != null;
2222
}
@@ -29,14 +29,14 @@ export class VideoSource implements definition.VideoSource {
2929
}
3030

3131
let videoURL = NSURL.URLWithString(fileName);
32-
let player = new AVPlayer(videoURL);
32+
let player = new AVPlayerItem(videoURL);
3333
this.ios = player;
3434
return this.ios != null;
3535
}
3636

3737
public loadFromUrl(url: string): boolean {
3838
let videoURL = NSURL.URLWithString(url);
39-
let player = new AVPlayer(videoURL);
39+
let player = new AVPlayerItem(videoURL);
4040
this.ios = player;
4141
return this.ios != null;
4242
}

videoplayer.ios.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import * as application from 'application';
1010

1111
declare var NSURL, AVPlayer, AVPlayerViewController, AVPlayerItemDidPlayToEndTimeNotification, UIView, CMTimeMakeWithSeconds, NSNotification, NSNotificationCenter, CMTimeGetSeconds, CMTimeMake, kCMTimeZero, AVPlayerItemStatusReadyToPlay;
1212

13-
1413
global.moduleMerge(common, exports);
1514

1615
function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
@@ -21,7 +20,6 @@ function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
2120
// register the setNativeValue callback
2221
(<proxy.PropertyMetadata>common.Video.videoSourceProperty.metadata).onSetNativeValue = onVideoSourcePropertyChanged;
2322

24-
2523
export class Video extends common.Video {
2624
private _player: any; /// AVPlayer
2725
private _playerController: any; /// AVPlayerViewController
@@ -32,20 +30,16 @@ export class Video extends common.Video {
3230
private _observer: NSObject;
3331
private _observerActive: boolean;
3432

35-
3633
constructor() {
3734
super();
38-
3935
this._playerController = new AVPlayerViewController();
40-
4136
this._player = new AVPlayer();
4237
this._playerController.player = this._player;
4338
// showsPlaybackControls must be set to false on init to avoid any potential 'Unable to simultaneously satisfy constraints' errors
4439
this._playerController.showsPlaybackControls = false;
4540
this._ios = this._playerController.view;
4641
this._observer = PlayerObserverClass.alloc();
4742
this._observer["_owner"] = this;
48-
4943
}
5044

5145
get ios(): any {
@@ -54,16 +48,26 @@ export class Video extends common.Video {
5448

5549
public _setNativeVideo(nativeVideoPlayer: any) {
5650
if (nativeVideoPlayer != null) {
57-
this._player = nativeVideoPlayer;
58-
this._init();
51+
let currentItem = this._player.currentItem;
52+
if (currentItem !== null) {
53+
// Need to set to null so the previous video is not shown while its loading
54+
this._player.replaceCurrentItemWithPlayerItem(null);
55+
this._removeStatusObserver(currentItem);
56+
this._addStatusObserver(nativeVideoPlayer);
57+
this._player.replaceCurrentItemWithPlayerItem(nativeVideoPlayer);
58+
}
59+
else {
60+
this._addStatusObserver(nativeVideoPlayer);
61+
this._player.replaceCurrentItemWithPlayerItem(nativeVideoPlayer);
62+
this._init();
63+
}
5964
}
6065
}
6166

6267
public _setNativePlayerSource(nativePlayerSrc: string) {
6368
this._src = nativePlayerSrc;
6469
let url: string = NSURL.URLWithString(this._src);
6570
this._player = new AVPlayer(url);
66-
6771
this._init();
6872
}
6973

@@ -85,11 +89,6 @@ export class Video extends common.Video {
8589
this._player.muted = true;
8690
}
8791

88-
if (!this._observerActive) {
89-
this._player.currentItem.addObserverForKeyPathOptionsContext(this._observer, "status", 0, null);
90-
this._observerActive = true;
91-
}
92-
9392
if (!this._didPlayToEndTimeActive) {
9493
this._didPlayToEndTimeObserver = application.ios.addNotificationObserver(AVPlayerItemDidPlayToEndTimeNotification, this.AVPlayerItemDidPlayToEndTimeNotification.bind(this));
9594
this._didPlayToEndTimeActive = true;
@@ -151,8 +150,7 @@ export class Video extends common.Video {
151150
}
152151

153152
if (this._observerActive = true) {
154-
this._player.currentItem.removeObserverForKeyPath(this._observer, "status");
155-
this._observerActive = false;
153+
this._removeStatusObserver(this._player.currentItem);
156154
}
157155
this.pause();
158156
this._player.replaceCurrentItemWithPlayerItem(null); //de-allocates the AVPlayer
@@ -163,6 +161,16 @@ export class Video extends common.Video {
163161
this._emit(common.Video.loadingCompleteEvent);
164162
}
165163

164+
private _addStatusObserver(currentItem) {
165+
currentItem.addObserverForKeyPathOptionsContext(this._observer, "status", 0, null);
166+
this._observerActive = true;
167+
}
168+
169+
private _removeStatusObserver(currentItem) {
170+
currentItem.removeObserverForKeyPath(this._observer, "status");
171+
this._observerActive = false;
172+
}
173+
166174
}
167175

168176
class PlayerObserverClass extends NSObject {

0 commit comments

Comments
 (0)