@@ -22,11 +22,11 @@ export class Video extends VideoBase {
2222 #playerView: com . google . android . exoplayer2 . ui . PlayerView ;
2323 #player: com . google . android . exoplayer2 . SimpleExoPlayer ;
2424 #playerListener: com . google . android . exoplayer2 . Player . EventListener ;
25+ #videoListener: com . google . android . exoplayer2 . video . VideoListener ;
2526 #src: string ;
2627 #autoplay: boolean ;
2728 #loop: boolean ;
2829 #textureView: android . view . TextureView ;
29-
3030 _isCustom : boolean = false ;
3131 _playing : boolean = false ;
3232 _timer : any ;
@@ -37,9 +37,12 @@ export class Video extends VideoBase {
3737 _canvas : any ;
3838 _frameTimer : any ;
3939 _readyState : number = 0 ;
40+ _videoWidth = 0 ;
41+ _videoHeight = 0 ;
4042 constructor ( ) {
4143 super ( ) ;
4244 java . lang . System . loadLibrary ( 'canvasnative' ) ;
45+ //@ts -ignore
4346 const builder = new com . google . android . exoplayer2 . SimpleExoPlayer . Builder ( Utils . ad . getApplicationContext ( ) ) ;
4447 this . #player = builder . build ( ) ;
4548 const ref = new WeakRef ( this ) ;
@@ -102,6 +105,17 @@ export class Video extends VideoBase {
102105 onTimelineChanged ( timeline : com . google . android . exoplayer2 . Timeline , manifest : any , reason : number ) { } ,
103106 onTracksChanged : function ( trackGroups , trackSelections ) { } ,
104107 } ) ;
108+ this . #videoListener = new com . google . android . exoplayer2 . video . VideoListener ( {
109+ onRenderedFirstFrame ( ) { } ,
110+ onVideoSizeChanged ( width : number , height : number , unappliedRotationDegrees : number , pixelWidthHeightRatio : number ) {
111+ const owner = ref . get ( ) ;
112+ if ( owner ) {
113+ owner . _videoWidth = width ;
114+ owner . _videoHeight = height ;
115+ }
116+ } ,
117+ onSurfaceSizeChanged ( width : number , height : number ) { } ,
118+ } ) ;
105119 const activity : androidx . appcompat . app . AppCompatActivity = Application . android . foregroundActivity || Application . android . startActivity ;
106120 const inflator = activity . getLayoutInflater ( ) ;
107121 const layout = Video . getResourceId ( Application . android . foregroundActivity || Application . android . startActivity , 'player' ) ;
@@ -111,9 +125,8 @@ export class Video extends VideoBase {
111125 const params = new android . widget . LinearLayout . LayoutParams ( android . widget . LinearLayout . LayoutParams . MATCH_PARENT , android . widget . LinearLayout . LayoutParams . MATCH_PARENT ) ;
112126 this . #textureView = new android . view . TextureView ( Application . android . foregroundActivity || Application . android . startActivity ) ;
113127 this . #container. addView ( this . #textureView as any , params ) ;
114- //this.#playerView.setPlayer(this.#player);
115- //this.#player.setVideoTextureView(this.#textureView);
116128 this . setNativeView ( this . #container) ;
129+ this . #player. addVideoListener ( this . #videoListener) ;
117130 }
118131
119132 get readyState ( ) {
@@ -139,7 +152,6 @@ export class Video extends VideoBase {
139152 }
140153 static st_count = 0 ;
141154 static createCustomView ( ) {
142- const canvas = require ( '@nativescript/canvas' ) ;
143155 const video = new Video ( ) ;
144156 video . _isCustom = true ;
145157 video . width = 300 ;
@@ -164,40 +176,48 @@ export class Video extends VideoBase {
164176
165177 _hasFrame = false ;
166178 getCurrentFrame ( context ?: WebGLRenderingContext ) {
167- const surfaceView = this . #playerView. getVideoSurfaceView ( ) ;
168- if ( surfaceView instanceof android . view . TextureView ) {
169- if ( ! this . _st ) {
170- // @ts -ignore
171- const result = com . github . triniwiz . canvas . Utils . createSurfaceTexture ( context ) ;
172- this . _st = result [ 0 ] ;
173- const ref = new WeakRef ( this ) ;
174- this . _frameListener = new android . graphics . SurfaceTexture . OnFrameAvailableListener ( {
175- onFrameAvailable ( param0 : android . graphics . SurfaceTexture ) {
176- const owner = ref . get ( ) ;
177- if ( owner ) {
178- owner . _hasFrame = true ;
179- owner . _notifyVideoFrameCallbacks ( ) ;
180- }
181- } ,
182- } ) ;
183- this . _st . setOnFrameAvailableListener ( this . _frameListener ) ;
184-
185- //this.#textureView.setSurfaceTexture(this._st);
186- this . _surface = new android . view . Surface ( this . _st ) ;
187- this . #player. setVideoSurface ( this . _surface ) ;
188- this . _render = result [ 1 ] ;
179+ if ( this . isLoaded ) {
180+ const surfaceView = this . #playerView. getVideoSurfaceView ( ) ;
181+ if ( surfaceView instanceof android . view . TextureView ) {
182+ const st = surfaceView . getSurfaceTexture ( ) ;
183+ if ( st ) {
184+ // @ts -ignore
185+ this . _render = com . github . triniwiz . canvas . Utils . createRenderAndAttachToGLContext ( context , st ) ;
186+ this . _st = st ;
187+ }
189188 }
189+ }
190190
191- if ( this . _st ) {
192- if ( ! this . _hasFrame ) {
193- return ;
194- }
191+ if ( ! this . _st ) {
192+ // @ts -ignore
193+ const result = com . github . triniwiz . canvas . Utils . createSurfaceTexture ( context ) ;
194+ this . _st = result [ 0 ] ;
195+ const ref = new WeakRef ( this ) ;
196+ this . _frameListener = new android . graphics . SurfaceTexture . OnFrameAvailableListener ( {
197+ onFrameAvailable ( param0 : android . graphics . SurfaceTexture ) {
198+ const owner = ref . get ( ) ;
199+ if ( owner ) {
200+ owner . _hasFrame = true ;
201+ owner . _notifyVideoFrameCallbacks ( ) ;
202+ }
203+ } ,
204+ } ) ;
195205
196- // @ts -ignore
197- com . github . triniwiz . canvas . Utils . updateTexImage ( context , this . _st , this . _render ) ;
206+ this . _st . setOnFrameAvailableListener ( this . _frameListener ) ;
198207
199- this . _hasFrame = false ;
208+ this . _surface = new android . view . Surface ( this . _st ) ;
209+ this . #player. setVideoSurface ( this . _surface ) ;
210+ this . _render = result [ 1 ] ;
211+ }
212+
213+ if ( this . _st ) {
214+ if ( ! this . _hasFrame ) {
215+ return ;
200216 }
217+ // @ts -ignore
218+ com . github . triniwiz . canvas . Utils . updateTexImage ( context , this . _st , this . _render , this . _videoWidth , this . _videoHeight , arguments [ 4 ] , arguments [ 5 ] ) ;
219+
220+ this . _hasFrame = false ;
201221 }
202222 }
203223
@@ -274,6 +294,7 @@ export class Video extends VideoBase {
274294 if ( ! this . #playerView) {
275295 this . #playerView = new com . google . android . exoplayer2 . ui . PlayerView ( this . _context ) ;
276296 }
297+ this . #playerView. setPlayer ( this . #player) ;
277298 return this . #playerView;
278299 }
279300
0 commit comments