Skip to content

Commit 254f22f

Browse files
committed
fix(media): improve initial load
1 parent a7488ef commit 254f22f

File tree

6 files changed

+38
-107
lines changed

6 files changed

+38
-107
lines changed

packages/canvas-media/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/canvas-media",
3-
"version": "0.9.0",
3+
"version": "0.10.0",
44
"description": "Canvas media",
55
"main": "index",
66
"typings": "index.d.ts",
Binary file not shown.

packages/canvas-media/platforms/android/java/org/nativescript/canvas_media/Utils.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/canvas-media/video/index.android.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
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';
33
import { Source } from '..';
44
import { isString } from '@nativescript/core/utils/types';
55

66
const STATE_IDLE: number = 1;
77
const STATE_BUFFERING: number = 2;
88
const STATE_READY: number = 3;
99
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 };
1912
export class Video extends VideoBase {
2013
#container: android.widget.LinearLayout;
2114
#sourceView: Source[] = [];
@@ -28,6 +21,13 @@ export class Video extends VideoBase {
2821
#loop: boolean;
2922
#textureView: android.view.TextureView;
3023

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;
3131
_isCustom: boolean = false;
3232
_playing: boolean = false;
3333
_timer: any;
@@ -41,22 +41,37 @@ export class Video extends VideoBase {
4141
_videoWidth = 0;
4242
_videoHeight = 0;
4343
private static _didInit = false;
44+
static BUFFER_MS = 500;
4445
constructor() {
4546
super();
4647
try {
4748
java.lang.System.loadLibrary('canvasnative');
4849
} catch (ex) {}
49-
50-
51-
//@ts-ignore
50+
51+
5252
const activity: androidx.appcompat.app.AppCompatActivity = Application.android.foregroundActivity || Application.android.startActivity;
5353

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;
5769
}
58-
5970
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());
6075
this.#player = builder.build();
6176
const ref = new WeakRef(this);
6277
this.#playerListener = new com.google.android.exoplayer2.Player.EventListener({
@@ -134,7 +149,7 @@ export class Video extends VideoBase {
134149
this.#player.addListener(this.#playerListener);
135150
this.#playerView = inflator.inflate(layout, null, false) as any; //new com.google.android.exoplayer2.ui.PlayerView(Application.android.foregroundActivity || Application.android.startActivity);
136151
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);
138153
this.#textureView = new android.view.TextureView(Application.android.foregroundActivity || Application.android.startActivity);
139154
this.#container.addView(this.#textureView as any, params);
140155
this.setNativeView(this.#container);
@@ -176,12 +191,7 @@ export class Video extends VideoBase {
176191
if (typeof value === 'string' && value.startsWith('~/')) {
177192
value = path.join(knownFolders.currentApp().path, value.replace('~', ''));
178193
}
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)));
185195
this.#player.prepare();
186196
if (this.#autoplay) {
187197
this.#player.setPlayWhenReady(true);

tools/demo/canvas/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export class DemoSharedCanvas extends DemoSharedBase {
318318

319319
draw() {
320320
//filterBlur(this.canvas);
321-
//handleVideo(this.canvas);
321+
handleVideo(this.canvas);
322322
// const worker = new CanvasWorker();
323323
// canvas.parent.on(GestureTypes.touch as any, (args: TouchGestureEventData) => {
324324
// var x = args.getX() * Screen.mainScreen.scale,
@@ -392,7 +392,7 @@ export class DemoSharedCanvas extends DemoSharedBase {
392392
//ellipse(this.canvas);
393393
// drawPatternWithCanvas(this.canvas);
394394
//this.clock(this.canvas);
395-
this.solar(this.canvas);
395+
//this.solar(this.canvas);
396396
//console.log('ready ??');
397397
//this.coloredParticles(this.canvas);
398398
//this.ball(this.canvas)

tools/demo/canvas/webgl/video.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function handleVideo(canvas) {
8282
const texture = initTexture(gl);
8383

8484
//https://github.com/mdn/webgl-examples/raw/gh-pages/tutorial/sample8/Firefox.mp4
85-
const video = setupVideo('https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4');
85+
const video = setupVideo('~/assets/file-assets/webgl/Firefox.mp4');
8686

8787
var then = 0;
8888

@@ -144,12 +144,6 @@ function setupVideo(url) {
144144
}
145145
}
146146

147-
// setTimeout(()=>{
148-
// video.pause();
149-
// setTimeout(()=>{
150-
// video.play();
151-
// },3000);
152-
// }, 10000);
153147
return video;
154148
}
155149

0 commit comments

Comments
 (0)