({ paused = false } = {})
| 311 | } |
| 312 | |
| 313 | override async apply({ paused = false } = {}): Promise<void> { |
| 314 | // Check if the audio should have a fade time |
| 315 | const fadePosition = this.props.indexOf('fade'); |
| 316 | const shouldLoop = this.props.indexOf('loop') > -1; |
| 317 | const volumePercentage = this.props.indexOf('volume') > -1 ? parseInt(this.props[this.props.indexOf('volume') + 1]) / 100 : 1; |
| 318 | const volume = this.mediaVolume * volumePercentage; |
| 319 | |
| 320 | // Create player if it doesn't exist yet |
| 321 | if (this.player === null) { |
| 322 | this.player = await this.createAudioPlayer(paused, volume); |
| 323 | this.engine.mediaPlayer(this.type, this.mediaKey, this.player); |
| 324 | } |
| 325 | |
| 326 | if (this.player instanceof AudioPlayer) { |
| 327 | // Make the audio loop if it was provided as a prop |
| 328 | if (shouldLoop) { |
| 329 | this.player.loop = true; |
| 330 | } |
| 331 | |
| 332 | // Set volume through the AudioPlayer setter |
| 333 | this.player.volume = volume; |
| 334 | this.player.dataset.volumePercentage = (volumePercentage * 100).toString(); |
| 335 | |
| 336 | this.player.onended = () => { |
| 337 | const endState: Record<string, any> = {}; |
| 338 | endState[this.type] = this.engine.state(this.type).filter((s: any) => s.statement !== this._statement); |
| 339 | this.engine.state(endState); |
| 340 | this.engine.removeMediaPlayer(this.type, this.mediaKey); |
| 341 | }; |
| 342 | |
| 343 | if (paused === true) { |
| 344 | return Promise.resolve(); |
| 345 | } |
| 346 | |
| 347 | // Start playback first, then apply fade if requested |
| 348 | await this.player.play(); |
| 349 | |
| 350 | if (fadePosition > -1) { |
| 351 | const fadeTime = this.props[fadePosition + 1]; |
| 352 | const match = fadeTime.match(/\d*(\.\d*)?/); |
| 353 | const duration = match ? parseFloat(match[0]) : 0; |
| 354 | // Don't await fadeIn - let it run in background while playback continues |
| 355 | this.player.fadeIn(duration, this.player.volume); |
| 356 | } |
| 357 | |
| 358 | return; |
| 359 | |
| 360 | } |
| 361 | else if (this.player instanceof Array) { |
| 362 | const promises = []; |
| 363 | for (const player of this.player) { |
| 364 | if (player.paused && !player.ended) { |
| 365 | if (fadePosition > -1) { |
| 366 | const fadeTime = this.props[fadePosition + 1]; |
| 367 | const match = fadeTime.match(/\d*(\.\d*)?/); |
| 368 | const duration = match ? parseFloat(match[0]) : 0; |
| 369 | player.fadeIn(duration); |
| 370 | } |
no test coverage detected