1
1
import { VideoBase , controlsProperty , autoplayProperty , loopProperty , srcProperty , currentTimeProperty , durationProperty } from './common' ;
2
- import { Screen , Application , ControlStateChangeListener , Utils , knownFolders , path } from '@nativescript/core' ;
2
+ import { Screen , Application , Utils , knownFolders , path } from '@nativescript/core' ;
3
3
import { Source } from '..' ;
4
4
import { isString } from '@nativescript/core/utils/types' ;
5
5
6
6
const STATE_IDLE : number = 1 ;
7
7
const STATE_BUFFERING : number = 2 ;
8
8
const STATE_READY : number = 3 ;
9
9
const STATE_ENDED : number = 4 ;
10
-
11
- // @NativeClass ()
12
- // class NativeOhax extends java.lang.Object {
13
- // finalize(){
14
- // // clean shit up
15
- // super.finalize();
16
- // }
17
- // }
18
-
10
+ const MATCH_PARENT = 0xffffffff ;
11
+ const TYPE = { DETECT : 0 , SS : 1 , DASH : 2 , HLS : 3 , OTHER : 4 } ;
19
12
export class Video extends VideoBase {
20
13
#container: android . widget . LinearLayout ;
21
14
#sourceView: Source [ ] = [ ] ;
@@ -28,6 +21,13 @@ export class Video extends VideoBase {
28
21
#loop: boolean ;
29
22
#textureView: android . view . TextureView ;
30
23
24
+ private static _cache : com . google . android . exoplayer2 . upstream . cache . SimpleCache ;
25
+ private static _leastRecentlyUsedCacheEvictor : com . google . android . exoplayer2 . upstream . cache . LeastRecentlyUsedCacheEvictor ;
26
+ private static _exoDatabaseProvider : com . google . android . exoplayer2 . database . ExoDatabaseProvider ;
27
+ private static _exoPlayerCacheSize = 100 * 1024 * 1024 ;
28
+ private static _dsf : com . google . android . exoplayer2 . upstream . DefaultDataSourceFactory ;
29
+ private static _msf : com . google . android . exoplayer2 . source . DefaultMediaSourceFactory ;
30
+ private static _cdsf : com . google . android . exoplayer2 . upstream . cache . CacheDataSourceFactory ;
31
31
_isCustom : boolean = false ;
32
32
_playing : boolean = false ;
33
33
_timer : any ;
@@ -41,22 +41,37 @@ export class Video extends VideoBase {
41
41
_videoWidth = 0 ;
42
42
_videoHeight = 0 ;
43
43
private static _didInit = false ;
44
+ static BUFFER_MS = 500 ;
44
45
constructor ( ) {
45
46
super ( ) ;
46
47
try {
47
48
java . lang . System . loadLibrary ( 'canvasnative' ) ;
48
49
} catch ( ex ) { }
49
-
50
-
51
- //@ts -ignore
50
+
51
+
52
52
const activity : androidx . appcompat . app . AppCompatActivity = Application . android . foregroundActivity || Application . android . startActivity ;
53
53
54
- if ( ! Video . _didInit ) {
55
- // @ts -ignore
56
- org . nativescript . canvas_media . Utils . init ( activity , path . join ( knownFolders . documents ( ) . path , 'MEDIA_PLAYER_CACHE' ) ) ;
54
+ if ( ! Video . _didInit ) {
55
+ const packageName = activity . getPackageName ( ) ;
56
+ const cacheDir = new java . io . File ( path . join ( knownFolders . documents ( ) . path , 'MEDIA_PLAYER_CACHE' ) ) ;
57
+ if ( ! cacheDir . exists ( ) ) {
58
+ cacheDir . mkdirs ( ) ;
59
+ }
60
+
61
+ Video . _leastRecentlyUsedCacheEvictor = new com . google . android . exoplayer2 . upstream . cache . LeastRecentlyUsedCacheEvictor ( Video . _exoPlayerCacheSize ) ;
62
+
63
+ Video . _exoDatabaseProvider = new com . google . android . exoplayer2 . database . ExoDatabaseProvider ( activity ) ;
64
+ Video . _cache = new com . google . android . exoplayer2 . upstream . cache . SimpleCache ( cacheDir , Video . _leastRecentlyUsedCacheEvictor , Video . _exoDatabaseProvider ) ;
65
+ Video . _dsf = new com . google . android . exoplayer2 . upstream . DefaultDataSourceFactory ( activity , com . google . android . exoplayer2 . util . Util . getUserAgent ( activity , packageName ) ) ;
66
+ Video . _cdsf = new com . google . android . exoplayer2 . upstream . cache . CacheDataSourceFactory ( Video . _cache , Video . _dsf )
67
+ Video . _msf = new com . google . android . exoplayer2 . source . DefaultMediaSourceFactory ( Video . _cdsf ) ;
68
+ Video . _didInit = true ;
57
69
}
58
-
59
70
const builder = new com . google . android . exoplayer2 . SimpleExoPlayer . Builder ( activity ) ;
71
+ builder . setMediaSourceFactory ( Video . _msf ) ;
72
+ const loadControl = new com . google . android . exoplayer2 . DefaultLoadControl . Builder ( ) ;
73
+ loadControl . setBufferDurationsMs ( Video . BUFFER_MS , com . google . android . exoplayer2 . DefaultLoadControl . DEFAULT_MAX_BUFFER_MS , Video . BUFFER_MS , Video . BUFFER_MS ) ;
74
+ builder . setLoadControl ( loadControl . build ( ) ) ;
60
75
this . #player = builder . build ( ) ;
61
76
const ref = new WeakRef ( this ) ;
62
77
this . #playerListener = new com . google . android . exoplayer2 . Player . EventListener ( {
@@ -134,7 +149,7 @@ export class Video extends VideoBase {
134
149
this . #player. addListener ( this . #playerListener) ;
135
150
this . #playerView = inflator . inflate ( layout , null , false ) as any ; //new com.google.android.exoplayer2.ui.PlayerView(Application.android.foregroundActivity || Application.android.startActivity);
136
151
this . #container = new android . widget . LinearLayout ( Application . android . foregroundActivity || Application . android . startActivity ) ;
137
- const params = new android . widget . LinearLayout . LayoutParams ( android . widget . LinearLayout . LayoutParams . MATCH_PARENT , android . widget . LinearLayout . LayoutParams . MATCH_PARENT ) ;
152
+ const params = new android . widget . LinearLayout . LayoutParams ( MATCH_PARENT , MATCH_PARENT ) ;
138
153
this . #textureView = new android . view . TextureView ( Application . android . foregroundActivity || Application . android . startActivity ) ;
139
154
this . #container. addView ( this . #textureView as any , params ) ;
140
155
this . setNativeView ( this . #container) ;
@@ -176,12 +191,7 @@ export class Video extends VideoBase {
176
191
if ( typeof value === 'string' && value . startsWith ( '~/' ) ) {
177
192
value = path . join ( knownFolders . currentApp ( ) . path , value . replace ( '~' , '' ) ) ;
178
193
}
179
- if ( typeof value === 'string' && value . startsWith ( 'http' ) ) {
180
- // @ts -ignore
181
- org . nativescript . canvas_media . Utils . cacheUrl ( Application . android . foregroundActivity || Application . android . startActivity , value ) ;
182
- }
183
-
184
- this . #player. addMediaItem ( com . google . android . exoplayer2 . MediaItem . fromUri ( android . net . Uri . parse ( value ) ) ) ;
194
+ this . #player. setMediaItem ( com . google . android . exoplayer2 . MediaItem . fromUri ( android . net . Uri . parse ( value ) ) ) ;
185
195
this . #player. prepare ( ) ;
186
196
if ( this . #autoplay) {
187
197
this . #player. setPlayWhenReady ( true ) ;
0 commit comments