@@ -10,7 +10,6 @@ import * as application from 'application';
10
10
11
11
declare var NSURL , AVPlayer , AVPlayerViewController , AVPlayerItemDidPlayToEndTimeNotification , UIView , CMTimeMakeWithSeconds , NSNotification , NSNotificationCenter , CMTimeGetSeconds , CMTimeMake , kCMTimeZero , AVPlayerItemStatusReadyToPlay ;
12
12
13
-
14
13
global . moduleMerge ( common , exports ) ;
15
14
16
15
function onVideoSourcePropertyChanged ( data : dependencyObservable . PropertyChangeData ) {
@@ -21,7 +20,6 @@ function onVideoSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
21
20
// register the setNativeValue callback
22
21
( < proxy . PropertyMetadata > common . Video . videoSourceProperty . metadata ) . onSetNativeValue = onVideoSourcePropertyChanged ;
23
22
24
-
25
23
export class Video extends common . Video {
26
24
private _player : any ; /// AVPlayer
27
25
private _playerController : any ; /// AVPlayerViewController
@@ -32,20 +30,16 @@ export class Video extends common.Video {
32
30
private _observer : NSObject ;
33
31
private _observerActive : boolean ;
34
32
35
-
36
33
constructor ( ) {
37
34
super ( ) ;
38
-
39
35
this . _playerController = new AVPlayerViewController ( ) ;
40
-
41
36
this . _player = new AVPlayer ( ) ;
42
37
this . _playerController . player = this . _player ;
43
38
// showsPlaybackControls must be set to false on init to avoid any potential 'Unable to simultaneously satisfy constraints' errors
44
39
this . _playerController . showsPlaybackControls = false ;
45
40
this . _ios = this . _playerController . view ;
46
41
this . _observer = PlayerObserverClass . alloc ( ) ;
47
42
this . _observer [ "_owner" ] = this ;
48
-
49
43
}
50
44
51
45
get ios ( ) : any {
@@ -54,16 +48,26 @@ export class Video extends common.Video {
54
48
55
49
public _setNativeVideo ( nativeVideoPlayer : any ) {
56
50
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
+ }
59
64
}
60
65
}
61
66
62
67
public _setNativePlayerSource ( nativePlayerSrc : string ) {
63
68
this . _src = nativePlayerSrc ;
64
69
let url : string = NSURL . URLWithString ( this . _src ) ;
65
70
this . _player = new AVPlayer ( url ) ;
66
-
67
71
this . _init ( ) ;
68
72
}
69
73
@@ -85,11 +89,6 @@ export class Video extends common.Video {
85
89
this . _player . muted = true ;
86
90
}
87
91
88
- if ( ! this . _observerActive ) {
89
- this . _player . currentItem . addObserverForKeyPathOptionsContext ( this . _observer , "status" , 0 , null ) ;
90
- this . _observerActive = true ;
91
- }
92
-
93
92
if ( ! this . _didPlayToEndTimeActive ) {
94
93
this . _didPlayToEndTimeObserver = application . ios . addNotificationObserver ( AVPlayerItemDidPlayToEndTimeNotification , this . AVPlayerItemDidPlayToEndTimeNotification . bind ( this ) ) ;
95
94
this . _didPlayToEndTimeActive = true ;
@@ -151,8 +150,7 @@ export class Video extends common.Video {
151
150
}
152
151
153
152
if ( this . _observerActive = true ) {
154
- this . _player . currentItem . removeObserverForKeyPath ( this . _observer , "status" ) ;
155
- this . _observerActive = false ;
153
+ this . _removeStatusObserver ( this . _player . currentItem ) ;
156
154
}
157
155
this . pause ( ) ;
158
156
this . _player . replaceCurrentItemWithPlayerItem ( null ) ; //de-allocates the AVPlayer
@@ -163,6 +161,16 @@ export class Video extends common.Video {
163
161
this . _emit ( common . Video . loadingCompleteEvent ) ;
164
162
}
165
163
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
+
166
174
}
167
175
168
176
class PlayerObserverClass extends NSObject {
0 commit comments