123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //AudioManager.ts
- import { Node, AudioSource, AudioClip, resources, director, assetManager } from 'cc';
- import { BundleName } from '../Config/EnumCfg';
- import { User } from './LocalDataMgr';
- /**
- * @en
- * this is a sington class for audio play, can be easily called from anywhere in you project.
- * @zh
- * 这是一个用于播放音频的单件类,可以很方便地在项目的任何地方调用。
- */
- export class AudioManager {
- private static _instance: AudioManager;
- public static get instance(): AudioManager {
- if (this._instance == null) {
- this._instance = new AudioManager();
- }
- return this._instance;
- }
- private _audioSource: AudioSource;
- yinxiaoValue: number = 1
- yinyueValue: number = 1
- constructor() {
- //@en create a node as AudioManager
- //@zh 创建一个节点作为 AudioManager
- let audioMgr = new Node();
- audioMgr.name = '__audioMgr__';
- //@en add to the scene.
- //@zh 添加节点到场景
- director.getScene().addChild(audioMgr);
- //@en make it as a persistent node, so it won't be destroied when scene change.
- //@zh 标记为常驻节点,这样场景切换的时候就不会被销毁了
- director.addPersistRootNode(audioMgr);
- //@en add AudioSource componrnt to play audios.
- //@zh 添加 AudioSource 组件,用于播放音频。
- this._audioSource = audioMgr.addComponent(AudioSource);
- this.yinxiaoValue = User.userData.sound ? 1 : 0
- this.yinyueValue = User.userData.music ? 1 : 0
- }
- public get audioSource() {
- return this._audioSource;
- }
- /**
- * @en
- * play short audio, such as strikes,explosions
- * @zh
- * 播放短音频,比如 打击音效,爆炸音效等
- * @param sound clip or url for the audio
- * @param volume
- */
- playOneShot(sound: AudioClip | string, volume: number = 1.0) {
- if (sound instanceof AudioClip) {
- this._audioSource.playOneShot(sound, volume * this.yinxiaoValue);
- }
- else {
- resources.load(sound, (err, clip: AudioClip) => {
- if (err) {
- console.log(err);
- }
- else {
- this._audioSource.playOneShot(clip, volume * this.yinxiaoValue);
- }
- });
- }
- }
- /**
- * @en
- * play long audio, such as the bg music
- * @zh
- * 播放长音频,比如 背景音乐
- * @param sound clip or url for the sound
- * @param volume
- */
- play(sound: AudioClip | string, volume: number = 1.0) {
- if (sound instanceof AudioClip) {
- this._audioSource.clip = sound;
- this._audioSource.play();
- this.audioSource.volume = volume;
- }
- else {
- resources.load(sound, (err, clip: AudioClip) => {
- if (err) {
- console.log(err);
- }
- else {
- this._audioSource.clip = clip;
- this.audioSource.volume = volume;
- this._audioSource.play();
- }
- });
- }
- }
- playBgm(sound: string) {
- let bundle = assetManager.getBundle(BundleName.hall)
- bundle.load('audio/' + sound, AudioClip, (err, clip) => {
- if (err) {
- console.error(err)
- }
- else {
- this._audioSource.stop()
- this._audioSource.clip = clip;
- this._audioSource.loop = true
- this._audioSource.play();
- this.audioSource.volume = this.yinyueValue;
- }
- })
- }
- playBundleAudio(sound: string) {
- let bundle = assetManager.getBundle(BundleName.hall)
- bundle.load('audio/' + sound, AudioClip, (err, clip) => {
- if (err) {
- console.error(err)
- }
- else {
- this._audioSource.playOneShot(clip, this.yinxiaoValue)
- }
- })
- }
- playOtherBundleAudio(bundlename: string, sound: string) {
- let bundle = assetManager.getBundle(bundlename)
- bundle.load('audio/' + sound, AudioClip, (err, clip) => {
- if (err) {
- console.error(err)
- }
- else {
- this._audioSource.playOneShot(clip, this.yinxiaoValue)
- }
- })
- }
- /**
- * stop the audio play
- */
- stop() {
- this._audioSource.stop();
- }
- /**
- * pause the audio play
- */
- pause() {
- this._audioSource.pause();
- }
- /**
- * resume the audio play
- */
- resume() {
- this._audioSource.play();
- }
- }
|