12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import { _decorator, AudioClip, AudioSource, Component, Node, resources } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('AudioManage')
- export class AudioManage extends Component {
- private static _instance: AudioManage;
- private static _audioSource?: AudioSource;
- static get instance() {
- if (this._instance) {
- return this._instance;
- }
- this._instance = new AudioManage();
- return this._instance;
- }
- protected onLoad(): void {
- this.init();
- }
- init() {
- AudioManage._audioSource = this.node.getComponent(AudioSource);
- }
- //播放音乐
- playMusic(loop: boolean = true) {
- const audioSource = AudioManage._audioSource!;
- audioSource.loop = loop;
- if (!audioSource.playing) {
- audioSource.play();
- }
- }
- //暂停音乐
- pauseMusic() {
- const audioSource = AudioManage._audioSource!;
- if (audioSource.playing) {
- audioSource.stop();
- }
- }
- /**
- * 播放音效
- * @param {String} name 音效名称
- * @param {Number} volumeScale 播放音量倍数
- */
- playSound(name: string, volumeScale: number = 1) {
- const audioSource = AudioManage._audioSource!;
- resources.load(name, AudioClip, (err: any, ac) => {
- if(ac){
- audioSource.playOneShot(ac, volumeScale);
- }
- });
- }
- setBgVolume(value: number) {
- const audioSource = AudioManage._audioSource!;
- audioSource.volume = value;
- }
- }
|