| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import { assetManager, AudioClip } from "cc";
- import { loadType } from "../../ch/audio/audio";
- import { ch } from "../../ch/ch";
- export class AudioManager {
- private get audio() { return ch.audio; }
- // 震动
- private _switch_vibrate: boolean = true;
- /**
- * @en
- * play short audio, such as strikes,explosions
- * @zh
- * 播放短音频,比如 打击音效,爆炸音效等
- * @param sound clip or url for the audio
- * @param interval 同名字音频限制播放间隔(毫秒) (默认:0不限制 特殊系数:>0 <=1 使用音频时间X此系数)
- */
- constructor(){
- const local_data = chsdk.storage.getObject("AudioManager_vibrate");
- if (local_data) {
- try {
- this._switch_vibrate = local_data._switch_vibrate
- }
- catch (e) {
- this._switch_vibrate = true
- }
- }
- else {
- this._switch_vibrate = true
- }
- }
- playOneShot(sound: AudioClip | string, interval: number = 0, remote_ext: string = '.mp3') {
-
- this.audio.playOneShot(sound, interval, remote_ext)
- }
- vibrateShort() {
- if (this._switch_vibrate) {
- chsdk.vibrateShort()
- }
- }
- vibrateLong() {
- if (this._switch_vibrate) {
- chsdk.vibrateLong()
- }
- }
- /**
- * @en
- * play long audio, such as the bg music
- * @zh
- * 播放长音频,比如 背景音乐
- * @param sound clip or url for the sound
- */
- currentSound = undefined
- play(sound: AudioClip | string, remote_ext: string = '.mp3') {
- this.currentSound = sound
- this.audio.play(sound, remote_ext)
- }
- /**
- * stop the audio play
- */
- stop() {
- this.audio.stop()
- }
- /**
- * pause the audio play
- */
- pause() {
- this.audio.pause()
- }
- /**
- * resume the audio play
- */
- resume() {
- this.audio.resume()
- }
- /** 重播当前音乐 */
- public replay_music(): void {
-
- this.audio.stop()
- if (this.currentSound) {
- this.play(this.currentSound)
- }
- // this.audio.replay_music()
- }
- init(load_type: loadType, bundle_name: string, remote_url: string): void {
- this.currentSound = undefined
- if (loadType.bundle == load_type) {
- assetManager.loadBundle(bundle_name)
- }
- this.audio.init(load_type, bundle_name, remote_url)
- }
- /**
- * 获取背景音乐开关值
- */
- get switchMusic(): boolean {
- return this.audio.switchMusic;
- }
- /**
- * 设置背景音乐开关值
- * @param value 开关值
- */
- set switchMusic(value: boolean) {
- this.audio.switchMusic = value
- }
- /**
- * 获取音效开关值
- */
- get switchEffect(): boolean {
- return this.audio.switchEffect;
- }
- /**
- * 设置音效开关值
- * @param value 音效开关值
- */
- set switchEffect(value: boolean) {
- this.audio.switchEffect = value
- }
- /**
- * 获取震动开关值
- */
- get switch_vibrate(): boolean {
- return this._switch_vibrate;
- }
- /**
- * 设置音效开关值
- * @param value 音效开关值
- */
- set switch_vibrate(value: boolean) {
- this._switch_vibrate= value
- }
- save() {
- const local_data:any={};
- local_data._switch_vibrate = this._switch_vibrate;
-
- chsdk.storage.set("AudioManager_vibrate",local_data);
- this.audio.save()
- }
- /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
- load() {
- this.audio.load()
- }
- }
- export const audioManager = new AudioManager
|