AudioResources.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { _EJsonRes } from "../Data/CommonDataType";
  2. import { EventName } from "../Event/EventName";
  3. import EventManager from "../Event/EventManager";
  4. import Tool from "../Tool/Tool";
  5. /**
  6. * 音频资源 - 数据层
  7. */
  8. export default class AudioResources {
  9. /**资源列表 */
  10. private static resources: Map<string, any> = new Map<string, any>();
  11. /**资源包 */
  12. private static bundle: cc.AssetManager.Bundle = null;
  13. /**加载音频资源 */
  14. public static loadAudioResources (audioRes: Array<string>, denominator: number, currentProgress: number, callback: Function) {
  15. Tool.log("加载音频资源");
  16. let complete = () => {
  17. Tool.log(this.resources);
  18. Tool.log("加载完毕");
  19. Tool.log("");
  20. if (callback == null) {
  21. // 发送初始加载完成 本地消息
  22. EventManager.sendEvent(EventName.Res.Res_LoadComplete);
  23. }
  24. else {
  25. callback();
  26. }
  27. }
  28. if (audioRes.length == 0) {
  29. complete();
  30. }
  31. else {
  32. cc.assetManager.loadBundle("res/audio", (err: Error, audio: cc.AssetManager.Bundle) => {
  33. this.bundle = audio;
  34. audio.load(audioRes, cc.Asset, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
  35. // 发送初始加载进度 本地消息
  36. EventManager.sendEvent(EventName.Res.Res_LoadProgress, (finish/total)*denominator + currentProgress);
  37. }, (err, assets: Array<cc.Asset>) => {
  38. Tool.log(err);
  39. for (let index in assets) {
  40. this.resources.set(assets[index].name, assets[index]);
  41. }
  42. complete();
  43. });
  44. });
  45. }
  46. }
  47. /**获得Audio资源 */
  48. public static getResources (name: string): any {
  49. let _name: string = name;
  50. if (name.includes("/") == true) {
  51. _name = name.substring(name.indexOf("/")+1);
  52. }
  53. if (this.resources.get(_name) != null) {
  54. return this.resources.get(_name);
  55. }
  56. else {
  57. return this.bundle.get(_name);
  58. }
  59. }
  60. }