import { AudioClip, AudioSource, error, _decorator } from 'cc'; import { resLoader } from '../res/ResLoader'; const { ccclass, menu } = _decorator; export class AudioMusic extends AudioSource { onComplete: Function = null; private _progress: number = 0; private _url: string = null; private _is_play: boolean = false; /** * 设置音乐当前播放进度 * @param progress 进度百分比(0~1) */ get progress() { this._progress = this.currentTime / this.duration; return this._progress; } set progress(value: number) { this._progress = value; this.currentTime = value * this.duration; } load(bundle: string, url: string, callback?: Function,loop: boolean = true) { resLoader.load(bundle, url, AudioClip, (error: Error, clip: AudioClip) => { if (error) { console.error(error); } else { if (this.playing) { this._is_play = false; this.stop(); // resLoader.release(this._url!, bundle); } this.clip = clip; this.currentTime = 0; this.loop = loop; this.play(); callback && callback(); this._url = url; } }); } update() { (this.currentTime > 0) && (this._is_play = true); if (this._is_play && this.playing == false) { this._is_play = false; this.onComplete && this.onComplete(); } } }