音频播放模块

小游戏平台管理集合(3)

音频播放模块

简单使用音频播放功能,测试工具(微信开发工具)

代码块

module manager {
	/**微信音频 - 【Android 上最多同时播放 10 个音频】*/
	export class InnerAudioManager {
		private static mIns: InnerAudioManager;
		public static get ins(): InnerAudioManager {
			if (this.mIns == null) {
				this.mIns = new InnerAudioManager();
			}
			return this.mIns;
		}

		public constructor() {
			this.mInnerAudioDlDic = {};
			this.mInnerAudioDdDic = {};
		}
		/**播放选项 */
		public setInnerAudioOption() {
			window["wx"].setInnerAudioOption({
				mixWithOther: true,//是否与其他音频混播
				obeyMuteSwitch: true//(仅在 iOS 生效)是否遵循静音开关
			});
		}
		/**单例式 【bgm】*/
		private mInnerAudioDlDic: any;
		public playMusic(_url: string, _loop: boolean = false) {
			if (this.mInnerAudioDlDic[_url] == null) {
				this.mInnerAudioDlDic[_url] = window["wx"].createInnerAudioContext();
				this.mInnerAudioDlDic[_url].src = "resource/assets/" + _url + ".mp3";
				this.mInnerAudioDlDic[_url].loop = _loop;
			}
			this.mInnerAudioDlDic[_url].stop();
			this.mInnerAudioDlDic[_url].play();
		}
		public playBGM() {
			if (this.mInnerAudioDlDic[InnerAudioType.BGM_GAME]) {
				this.mInnerAudioDlDic[InnerAudioType.BGM_GAME].play();
			} else {
				this.playMusic(InnerAudioType.BGM_GAME, true);
				window["wx"].onAudioInterruptionEnd(() => {
					this.playBGM();
				});
			}
		}
		public stopBGM() {
			if (this.mInnerAudioDlDic[InnerAudioType.BGM_GAME]) {
				this.mInnerAudioDlDic[InnerAudioType.BGM_GAME].pause();
			}
		}
		/**单独式 【播放完毕 释放或回收】*/
		private mInnerAudioDdDic: any;
		public playMusicAuto(_url: string, _destory: boolean = false) {
			if (this.mInnerAudioDdDic[_url] == null) {
				this.mInnerAudioDdDic[_url] = [];
			}
			let tmpInnerAudio: any;
			if (this.mInnerAudioDdDic[_url].length > 2) {
				tmpInnerAudio = this.mInnerAudioDdDic[_url].pop();
			} else {
				tmpInnerAudio = window["wx"].createInnerAudioContext();
				tmpInnerAudio.src = "resource/assets/" + _url + ".mp3";
			}
			tmpInnerAudio.play();
			/**音频自然播放至结束 */
			tmpInnerAudio.onEnded(() => {
				tmpInnerAudio.offEnded();
				if (_destory) {
					tmpInnerAudio.destroy();
				} else {
					this.mInnerAudioDdDic[_url].push(tmpInnerAudio);
				}
			});
		}
	}
	export class InnerAudioType {
		public static BGM_GAME = "bgm_game";//游戏bgm
		public static UPGRADE_TANK = "upgrade_tank";//升级坦克
	}
}

说明

我粘贴的是简单的流程操作,可直接使用。

大概说一下项目之前碰到的问题:
1.微信环境下,音频放分包中,不用分包加载依然可以直接填充路径进行播放
2.单例式适用于,每种音频被反复操作
3.单独式适用于,每种音频都是独立模块,互不影响

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值