import { Component } from "../core/Component" ; import { ResManager } from "../core/ResManager" ; import { TweenManager } from "../tween/TweenManager" ; import { Tween } from "../tween/Tween" ; import { Log } from "../log/Log" ; import { AudioManager } from "./AudioManager" ; import { HTMLAudio } from "./html/HTMLAudio" ; export class AudioComponent extends Component { public static END : string = "end" ; public static PLAY : string = "play" ; public static PROGRESS : string = "progress" ; /**
     * 事件
     */ public static EVENTS = HTMLAudio . EVENTS ; public nick : string = "" ; public destroyAudio : boolean = false ; public index = 0 ; public count = 0 ; private audioSource : HTMLAudio ; private isPlayingActive : boolean = false ; private _offsetVolume : number = 1 ; private _aminVolume : number = 1 ; private _offsetMuted : boolean = false ; private _muted : boolean = false ; private _volume : number = 1 ; public audioSourceName : string ; private tweenVolume : Tween | undefined ; instance : any ; private _globalVolume : number = 1 ; private _globalSpeed : number = 1 ; private _speed : number = 1 ; private _globalMuted : boolean = false ; isPause : boolean = false ; isGlobalPause : boolean = false ; constructor ( name ) { super ( ) ; this . audioSourceName = name ; this . audioSource = < any > AudioManager . play ( name ) ; } get active ( ) : boolean { return this . _active ; } /**
     * 设置激活状态
     */ set active ( active ) { this . _active = active ; if ( active ) { if ( this . isPlayingActive ) { this . resume ( ) ; } } else { this . isPlayingActive = this . isPlaying ; this . pause ( ) ; } active ? this . activated ( ) : this . deactivated ( ) ; } get isPlaying ( ) : boolean { return false ; } /**
     * 全局音量
     */ set globalVolume ( volume ) { this . _globalVolume = volume ; this . volume = this . volume ; } get globalVolume ( ) : number { return this . _globalVolume ; } /**
     * 偏移音量
     */ set offsetVolume ( volume ) { this . _offsetVolume = volume ; this . volume = this . volume ; } get offsetVolume ( ) : number { return this . _offsetVolume ; } /**
     * 声音渐变动画的音量
     */ set aminVolume ( volume ) { this . _aminVolume = volume ; this . volume = this . volume ; } get aminVolume ( ) : number { return this . _aminVolume ; } /**
     * 音量
     */ set volume ( volume ) { this . _volume = volume ; let v = this . _volume * this . offsetVolume * this . aminVolume * this . globalVolume ; this . audioSource . volume = v ; } get volume ( ) : number { return this . _volume ; } /**
     * 全局静音
     */ set globalMuted ( muted ) { this . _globalMuted = muted ; this . muted = this . muted ; } get globalMuted ( ) : boolean { return this . _globalMuted ; } /**
     * 偏移静音
     */ set offsetMuted ( muted ) { this . _offsetMuted = muted ; this . muted = this . muted ; } get offsetMuted ( ) : boolean { return this . _offsetMuted ; } /**
     * 静音
     */ set muted ( muted ) { this . audioSource . muted = muted ; } get muted ( ) : boolean { return this . _muted ; } set loop ( loop ) { this . audioSource . loop = loop ; } get loop ( ) : boolean { return this . audioSource . loop ; } /**
     * 当前速度
     */ set speed ( speed ) { if ( ! this . instance ) { return ; } this . audioSource . speed = speed ; } get speed ( ) : number { return this . audioSource . speed ; } play ( ) { if ( ! this . active ) { console . warn ( "当前音频未激活，无法播放！" ) ; return ; } this . emit ( AudioComponent . PLAY ) ; this . globalVolume = AudioManager . globalVolume ; this . _globalSpeed = AudioManager . globalSpeed ; this . isGlobalPause = AudioManager . isGlobalPause ; this . audioSource . play ( ) ; this . speed = this . speed ; this . preRause ( ) ; } stop ( ) { if ( ! this . instance ) { return ; } this . audioSource . stop ( ) ; this . instance = undefined ; } /**
     * 暂停音乐
     */ pause ( ) { if ( ! this . isPlaying || ! this . instance ) { return ; } this . isPause = true ; this . preRause ( ) ; } private preRause ( ) { if ( this . isPause || ( this . isGlobalPause && this . instance ) ) { this . audioSource . pause ( ) ; } } private preResume ( ) { if ( ! this . isGlobalPause && ! this . isPause && this . instance ) { this . audioSource . play ( ) ; } } /**
     * 全局暂停音乐
     */ globalPause ( ) { if ( ! this . isPlaying || ! this . instance ) { return ; } this . isGlobalPause = true ; this . preRause ( ) ; } /**
     * 全局恢复
     */ globalResume ( ) { if ( ! this . isGlobalPause || ! this . instance ) { return ; } if ( ! this . active ) { console . warn ( "当前音频未激活，无法播放！" ) ; return ; } this . isGlobalPause = false ; this . preResume ( ) ; } /**
     * 恢复音乐
     */ resume ( ) { if ( this . isPlaying || ! this . isPause || ! this . instance ) { return ; } if ( ! this . active ) { console . warn ( "当前音频未激活，无法播放！" ) ; return ; } this . isPause = false ; this . preResume ( ) ; } fadeInVolume ( time , callback ? ) { this . fadeVolume ( time , this . aminVolume , 1 , callback ) ; } fadeOutVolume ( time , callback ? ) { this . fadeVolume ( time , this . aminVolume , 0 , callback ) ; } fadeToVolume ( time , volume , callback ? ) { this . fadeVolume ( time , this . aminVolume , volume , callback ) ; } fadeVolume ( time , startVolume , endVolume , callback ? ) { if ( this . tweenVolume ) { Log . warn ( "正在执行音乐的   fadeVolume 动画  无法重复执行  ，执行之前请先调用  stopVolume" ) ; return ; } this . tweenVolume = TweenManager . builder ( this ) . from ( { aminVolume : startVolume } ) . to ( { aminVolume : endVolume } ) . time ( time ) . setExpire ( true ) . on ( "end" , ( ) => { this . stopVolume ( ) ; callback && callback ( ) ; } ) . start ( ) ; } stopVolume ( ) { if ( this . tweenVolume ) { this . tweenVolume . stop ( ) ; this . tweenVolume = undefined ; } } removeEvents ( ) { super . removeEvents ( ) ; if ( this . instance ) { this . instance . off ( AudioComponent . PLAY ) ; this . instance . off ( AudioComponent . END ) ; this . instance . off ( AudioComponent . PROGRESS ) ; } } destroy ( options ? ) { let destroyAudio = ! ! options ; if ( this . isDestroy ) { return ; } if ( destroyAudio == undefined ) { destroyAudio = this . destroyAudio ; } if ( this . instance ) { this . instance . off ( AudioComponent . PLAY ) ; this . instance . off ( AudioComponent . END ) ; this . instance . off ( AudioComponent . PROGRESS ) ; } this . stop ( ) ; this . instance = undefined ; if ( destroyAudio ) { ResManager . removeResource ( this . audioSourceName ) ; } ( < any > this ) . filtersProperty = [ ] ; super . destroy ( ) ; } }