|
1 | | -import {AudioBase} from "./common"; |
| 1 | +import { AudioBase } from './common'; |
| 2 | +import { Application, booleanConverter, knownFolders, path } from '@nativescript/core'; |
| 3 | +import { Source } from '..'; |
| 4 | +import { durationProperty, currentTimeProperty } from '../common'; |
2 | 5 |
|
3 | | -export class Audio extends AudioBase {} |
| 6 | +declare var org: any; |
| 7 | + |
| 8 | +export class Audio extends AudioBase { |
| 9 | + _instance: any; |
| 10 | + _playResolve: Function | null = null; |
| 11 | + _playReject: Function | null = null; |
| 12 | + _playPromise: Promise<void> | null = null; |
| 13 | + _sourceView: Source[] = []; |
| 14 | + _playing: boolean = false; |
| 15 | + _readyState: number = 0; |
| 16 | + _isCustom: boolean = false; |
| 17 | + |
| 18 | + static createCustomView() { |
| 19 | + const audio = new Audio(); |
| 20 | + audio._isCustom = true; |
| 21 | + audio.width = 300; |
| 22 | + audio.height = 40; |
| 23 | + return audio; |
| 24 | + } |
| 25 | + |
| 26 | + constructor() { |
| 27 | + super(); |
| 28 | + |
| 29 | + const activity: any = Application.android.foregroundActivity || Application.android.startActivity; |
| 30 | + this._instance = new org.nativescript.canvas.media.AudioHelper(activity, knownFolders.documents().path); |
| 31 | + |
| 32 | + const ref = new WeakRef(this); |
| 33 | + |
| 34 | + this._instance.setCallback( |
| 35 | + new org.nativescript.canvas.media.AudioHelper.Callback({ |
| 36 | + onDurationChange(duration: number) { |
| 37 | + const owner = ref.get(); |
| 38 | + if (owner) { |
| 39 | + let secs = Number(duration) > 0 ? Number(duration) / 1000.0 : NaN; |
| 40 | + durationProperty.nativeValueChange(owner, secs); |
| 41 | + owner._notifyListener(Audio.durationchangeEvent); |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + onPlaying() { |
| 46 | + const owner = ref.get(); |
| 47 | + if (owner) { |
| 48 | + owner._playing = true; |
| 49 | + if (owner._playResolve) { |
| 50 | + owner._playResolve(); |
| 51 | + owner._playResolve = null; |
| 52 | + owner._playReject = null; |
| 53 | + owner._playPromise = null; |
| 54 | + } |
| 55 | + owner._notifyListener(Audio.playingEvent); |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + onCurrentTimeChanged(time: number) { |
| 60 | + const owner = ref.get(); |
| 61 | + if (owner) { |
| 62 | + let secs = Number(time) / 1000.0; |
| 63 | + currentTimeProperty.nativeValueChange(owner, secs); |
| 64 | + owner._notifyListener(Audio.timeupdateEvent); |
| 65 | + } |
| 66 | + }, |
| 67 | + |
| 68 | + onLoadedData() { |
| 69 | + const owner = ref.get(); |
| 70 | + if (owner) { |
| 71 | + owner._readyState = Audio.HAVE_CURRENT_DATA; |
| 72 | + owner._notifyListener(Audio.durationchangeEvent); |
| 73 | + owner._notifyListener(Audio.loadedmetadataEvent); |
| 74 | + owner._notifyListener(Audio.loadeddataEvent); |
| 75 | + } |
| 76 | + }, |
| 77 | + |
| 78 | + onCanPlay() { |
| 79 | + const owner = ref.get(); |
| 80 | + if (owner) { |
| 81 | + owner._notifyListener(Audio.canplayEvent); |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + onCanPlayThrough() { |
| 86 | + const owner = ref.get(); |
| 87 | + if (owner) { |
| 88 | + owner._notifyListener(Audio.canplaythroughEvent); |
| 89 | + } |
| 90 | + }, |
| 91 | + |
| 92 | + onError(message: string) { |
| 93 | + const owner = ref.get(); |
| 94 | + if (owner) { |
| 95 | + owner._playing = false; |
| 96 | + if (owner._playReject) { |
| 97 | + owner._playReject(new Error(message ?? 'Playback error')); |
| 98 | + } |
| 99 | + owner._playResolve = null; |
| 100 | + owner._playReject = null; |
| 101 | + owner._playPromise = null; |
| 102 | + owner._notifyListener(Audio.errorEvent); |
| 103 | + } |
| 104 | + }, |
| 105 | + }), |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + createNativeView() { |
| 110 | + return this._instance.getContainer(); |
| 111 | + } |
| 112 | + |
| 113 | + play() { |
| 114 | + if (this._playing) { |
| 115 | + return Promise.resolve(); |
| 116 | + } |
| 117 | + |
| 118 | + if (this._playPromise) { |
| 119 | + return this._playPromise; |
| 120 | + } |
| 121 | + |
| 122 | + this._playPromise = new Promise<void>((resolve, reject) => { |
| 123 | + this._playResolve = resolve; |
| 124 | + this._playReject = reject; |
| 125 | + try { |
| 126 | + this._instance.play(); |
| 127 | + } catch (e) { |
| 128 | + this._playResolve = null; |
| 129 | + this._playReject = null; |
| 130 | + this._playPromise = null; |
| 131 | + reject(e); |
| 132 | + } |
| 133 | + }); |
| 134 | + return this._playPromise; |
| 135 | + } |
| 136 | + |
| 137 | + pause() { |
| 138 | + if (!this._instance) { |
| 139 | + return; |
| 140 | + } |
| 141 | + this._instance.pause(); |
| 142 | + } |
| 143 | + |
| 144 | + get muted() { |
| 145 | + return this._instance.getMuted(); |
| 146 | + } |
| 147 | + |
| 148 | + set muted(value: boolean) { |
| 149 | + this._instance.setMuted(booleanConverter(value)); |
| 150 | + } |
| 151 | + |
| 152 | + get duration() { |
| 153 | + try { |
| 154 | + const d = this._instance.getDuration(); |
| 155 | + if (d == null || Number(d) <= 0) return NaN; |
| 156 | + return Number(d) / 1000.0; |
| 157 | + } catch (e) { |
| 158 | + return NaN; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + get currentTime() { |
| 163 | + try { |
| 164 | + return this._instance.getCurrentTime(); |
| 165 | + } catch (e) { |
| 166 | + return 0; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + set currentTime(value: number) { |
| 171 | + try { |
| 172 | + this._instance.setCurrentTime(value); |
| 173 | + } catch (e) {} |
| 174 | + } |
| 175 | + |
| 176 | + get src() { |
| 177 | + try { |
| 178 | + return this._instance.getSrc(); |
| 179 | + } catch (e) { |
| 180 | + return undefined; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + set src(value: string) { |
| 185 | + try { |
| 186 | + if (typeof value === 'string' && value.startsWith('~/')) { |
| 187 | + value = path.join(knownFolders.currentApp().path, value.replace('~', '')); |
| 188 | + } |
| 189 | + this._instance.setSrc(value); |
| 190 | + } catch (e) {} |
| 191 | + } |
| 192 | + |
| 193 | + load() { |
| 194 | + if (!this._instance) { |
| 195 | + return; |
| 196 | + } |
| 197 | + this._instance.load(); |
| 198 | + } |
| 199 | + |
| 200 | + canPlayType(type: string) { |
| 201 | + if (!this._instance) { |
| 202 | + return ''; |
| 203 | + } |
| 204 | + return this._instance.canPlayType(type); |
| 205 | + } |
| 206 | + |
| 207 | + get autoplay() { |
| 208 | + try { |
| 209 | + return this._instance.getAutoplay(); |
| 210 | + } catch (e) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + set autoplay(value: boolean) { |
| 216 | + this._instance.setAutoplay(booleanConverter(value)); |
| 217 | + } |
| 218 | + |
| 219 | + get controls() { |
| 220 | + try { |
| 221 | + return this._instance.getControls(); |
| 222 | + } catch (e) { |
| 223 | + return false; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + set controls(enabled: boolean) { |
| 228 | + this._instance.setControls(booleanConverter(enabled)); |
| 229 | + } |
| 230 | + |
| 231 | + get loop() { |
| 232 | + return this._instance.getLoop(); |
| 233 | + } |
| 234 | + |
| 235 | + set loop(value: boolean) { |
| 236 | + this._instance.setLoop(booleanConverter(value)); |
| 237 | + } |
| 238 | + |
| 239 | + _addChildFromBuilder(name: string, value: any) { |
| 240 | + if (value instanceof Source) { |
| 241 | + this._sourceView.push(value); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + onLoaded() { |
| 246 | + super.onLoaded(); |
| 247 | + if (this._sourceView.length > 0) { |
| 248 | + this.src = this._sourceView[0].src; |
| 249 | + } |
| 250 | + } |
| 251 | +} |
0 commit comments