cocos 播放配音

cocos 播放配音,第1张

概述首先一个配音id集合的json文件,AudioConfig.json: 1 [ 2 {"id":1,"path":"bgm/menuBgm"}, 3 {"id":2,"path":"bgm/BGM_001"}, 4 {"id":3,"path":"bgm/BGM_002"}, 5 {"id":4,"path":"bgm/BGM_003"}, 6

首先一个配音ID集合的Json文件,AudioConfig.Json:

 1 [ 2     {"ID":1,"path":"bgm/menuBgm"}, 3     {"ID":2,"path":"bgm/BGM_001"}, 4     {"ID":3,"path":"bgm/BGM_002"}, 5     {"ID":4,"path":"bgm/BGM_003"}, 6     {"ID":5,"path":"bgm/BGM_004"}, 7     {"ID":6,"path":"bgm/BGM_005"}, 8     {"ID":7,"path":"bgm/BGM_006"}, 9     {"ID":8,"path":"bgm/BGM_007"},10     {"ID":9,"path":"bgm/AnnIE_hello"},11     {"ID":10,"path":"bgm/Timmy_Hi_AnnIE_How_are_you_today"}12 ]

Json文件加载处理,配音调用类PlaySound类:

 1 cc.Class({ 2     extends: cc.Component, 3  4     propertIEs: { 5         _musicVolume: 1.0, 6         _playEffect: [], 7         _switchEffect: true 8     }, 9 10     onLoad () {11         cc.loader.load(cc.url.raw(‘resources/AudioConfig.Json‘),(err,object) => {12 13             if (err) {14                 cc.log(err);15             }16             else{17                 cc.log("我有数据啦");18                 var List = object;19 20                 var nlength = List.length;21                 // cc.log("nlength = " + nlength);22                 this.Arr = new Array(nlength);23                 for (let i=0;i<nlength;i++)24                 {25                     var arrvalue = List[i];26                     // 把数组当字典使用27                     this.Arr[arrvalue.ID] = arrvalue.path;28                 }29 30 31             }32         });33     },34 35 36     start () {37 38     },39 40     PlaySoundID: function(ID) {41         var _url = "audio/" + this.Arr[ID];42         // cc.log("PlaySoundID将要获取的ID对应路径:" + str);43         // loadRes 不要.mp3后缀名,如果有后缀名则警告44         cc.loader.loadRes(_url,asset)=> {45             this.playEffect(_url);46         });47     },48 49     playEffect: function(url){50         if(this._switchEffect){51             var rawUrl = cc.url.raw("resources/" + url);52             cc.log("resources/"+url);53            54             // getRes 要.mp3后缀名,否则要报警告55             if(cc.loader.getRes(rawUrl+".mp3")){56                 var effectID = cc.audioEngine.playEffect(rawUrl+".mp3",false,this._musicVolume);57                 this._playEffect[url] = effectID;58                 cc.log("声音播放");59             }60             else{61                 cc.log("【音频】音效" + url + "文件不存在");62             }63         }64 65 66     },67     // update (dt) {},68 });

配音文件在初始化时,既已调用,要把脚本添加到要添加的节点上,推荐添加到根节点,通过这个节点,进行一次加载即可在其他需要播放声音的时候,直接调用即可。

总结

以上是内存溢出为你收集整理的cocos 播放配音全部内容,希望文章能够帮你解决cocos 播放配音所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/999944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存