cocos2d-html5 各平台声音播放总结

来源:互联网 发布:微信支付退款接口 php 编辑:程序博客网 时间:2024/06/05 06:33

cocos2d-js 采用官方的CocosDesion播放声音

声音资源一部分是mp3,一部分ogg

PC端,谷歌浏览器,播放声音一切正常,但到手机浏览器上声音就不能正常播放了

根据官方文档的音乐支持格式

平台支持的常见文件格式备注Androidmp3, mid, oggg, wav可以播放android.media.MediaPlayer所支持的所有格式iOSaac, caf, mp3, m4a, wav可以播放AVAudioPlayer所支持的所有格式Windowsmid, mp3, wav无

CocosDesion支持的音效格式如下:

平台支持的常见文件格式备注Androidoggg, wav对wav的支持不完美iOScaf, m4a可以播放Cocos2d-iPhone CocosDesion所支持的所有格式Windowsmid, wav无主要原因是各平台格式不一样导致了不能播放

对比了一下音乐格式,主要采用ogg作为主要格式,其他格式作为扩展

 mp3 :体积小,音质好

 ogg :体积小,免费,音质好

 wav:无压缩、体积大

写了个测试程序

var HelloWorldLayer = cc.Layer.extend({    sprite:null,    _userCursor:null,    ctor:function () {        this._super();        var size = cc.winSize;        this.musicDirectory = new Array();        for(var i in music_res)        {            this.musicDirectory.push(music_res[i])        }        this.curMusicIndex = 0;        this.soundDirectory = new Array();        for(var i in sound_res)        {            this.soundDirectory.push(sound_res[i]);        }        this.curSoundIndex = 0;        var curMusic = new cc.LabelTTF("当前播放音乐:"+this.musicDirectory[this.curMusicIndex].substr(16), "微软雅黑", 20);        curMusic.x = cc.winSize.width / 2;        curMusic.y = cc.winSize.height / 2 + 150;        this.addChild(curMusic);        this.m_curMusicLabel = curMusic;        var playMusic = new cc.LabelTTF("播放", "微软雅黑", 18);        var playMusicItem = new cc.MenuItemLabel(playMusic, this.playMusicCallback, this);        playMusicItem.x = cc.winSize.width / 2 - 90;        playMusicItem.y = cc.winSize.height / 2 + 50;        var stopMusic = new cc.LabelTTF("停止", "微软雅黑", 18);        var stopMusicItem = new cc.MenuItemLabel(stopMusic, this.stopMusicCallback, this);        stopMusicItem.x = cc.winSize.width / 2 - 30;        stopMusicItem.y = cc.winSize.height / 2 + 50;        var prevMusic = new cc.LabelTTF("上一首", "微软雅黑", 18);        var prevMusicItem = new cc.MenuItemLabel(prevMusic, this.prevMusicCallback, this);        prevMusicItem.x = cc.winSize.width / 2 + 30;        prevMusicItem.y = cc.winSize.height / 2 + 50;        var nextMusic = new cc.LabelTTF("下一首", "微软雅黑", 18);        var nextMusicItem = new cc.MenuItemLabel(nextMusic, this.nextMusicCallback, this);        nextMusicItem.x = cc.winSize.width / 2 + 90;        nextMusicItem.y = cc.winSize.height / 2 + 50;        var curSound = new cc.LabelTTF("当前播放音效:"+this.soundDirectory[this.curSoundIndex].substr(22), "微软雅黑", 20);        curSound.x = cc.winSize.width / 2;        curSound.y = cc.winSize.height / 2 - 50;        this.addChild(curSound);        this.m_curSoundLabel = curSound;        var playSound = new cc.LabelTTF("播放", "微软雅黑", 18);        var playSoundItem = new cc.MenuItemLabel(playSound, this.playSoundCallback, this);        playSoundItem.x = cc.winSize.width / 2 - 90;        playSoundItem.y = cc.winSize.height / 2 - 150;        var stopSound = new cc.LabelTTF("停止", "微软雅黑", 18);        var stopSoundItem = new cc.MenuItemLabel(stopSound, this.stopSoundCallback, this);        stopSoundItem.x = cc.winSize.width / 2 - 30;        stopSoundItem.y = cc.winSize.height / 2 - 150;        var prevSound = new cc.LabelTTF("上一首", "微软雅黑", 18);        var prevSoundItem = new cc.MenuItemLabel(prevSound, this.prevSoundCallback, this);        prevSoundItem.x = cc.winSize.width / 2 + 30;        prevSoundItem.y = cc.winSize.height / 2 - 150;        var nextSound = new cc.LabelTTF("下一首", "微软雅黑", 18);        var nextSoundItem = new cc.MenuItemLabel(nextSound, this.nextSoundCallback, this);        nextSoundItem.x = cc.winSize.width / 2 + 90;        nextSoundItem.y = cc.winSize.height / 2 - 150;        var menu = new cc.Menu(prevMusicItem, nextMusicItem, playMusicItem, stopMusicItem,            prevSoundItem, nextSoundItem, playSoundItem, stopSoundItem);        menu.x = 0;        menu.y = 0;        this.addChild(menu);        return true;    },    prevMusicCallback:function(sender){        if(this.curMusicIndex == 0)        {            this.curMusicIndex = this.musicDirectory.length-1;        }        else        {            this.curMusicIndex--;        }        cc.audioEngine.playMusic(this.musicDirectory[this.curMusicIndex], false);        this.m_curMusicLabel.setString("当前播放音乐:"+this.musicDirectory[this.curMusicIndex].substr(16));    },    nextMusicCallback:function(sender){        this.curMusicIndex++;        if(this.curMusicIndex == this.musicDirectory.length)        {            this.curMusicIndex = 0;        }        cc.audioEngine.playMusic(this.musicDirectory[this.curMusicIndex], false);        this.m_curMusicLabel.setString("当前播放音乐:"+this.musicDirectory[this.curMusicIndex].substr(16));    },    playMusicCallback:function(sender){        cc.audioEngine.playMusic(this.musicDirectory[this.curMusicIndex], false);        this.m_curMusicLabel.setString("当前播放音乐:"+this.musicDirectory[this.curMusicIndex].substr(16));    },    stopMusicCallback:function(sender){        cc.audioEngine.stopMusic();        this.m_curMusicLabel.setString("音乐播放停止");    },    prevSoundCallback:function(sender){        if(this.curSoundIndex == 0)        {            this.curSoundIndex = this.soundDirectory.length-1;        }        else        {            this.curSoundIndex--;        }        cc.audioEngine.playEffect(this.soundDirectory[this.curSoundIndex], false);        this.m_curSoundLabel.setString("当前播放音效:"+this.soundDirectory[this.curSoundIndex].substr(22));    },    nextSoundCallback:function(sender){        this.curSoundIndex++;        if(this.curSoundIndex == this.soundDirectory.length)        {            this.curSoundIndex = 0;        }        cc.audioEngine.playEffect(this.soundDirectory[this.curSoundIndex], false);        this.m_curSoundLabel.setString("当前播放音效:"+this.soundDirectory[this.curSoundIndex].substr(22));    },    playSoundCallback:function(sender){        cc.audioEngine.playEffect(this.soundDirectory[this.curSoundIndex], false);        this.m_curSoundLabel.setString("当前播放音效:"+this.soundDirectory[this.curSoundIndex].substr(22));    },    stopSoundCallback:function(sender){        cc.audioEngine.stopEffect();        this.m_curSoundLabel.setString("播放音效停止");    }});
结果是,除了ios端有一个ogg播放不了,这个音乐播放小程序在各个平台ogg都能正常播放。

但放到我的游戏项目中结果又不一样了

----PC浏览器,音乐音效采用ogg可以正常播放

----安卓端浏览器,音乐音效采用ogg可以正常播放

----IOS端浏览器,音乐无法播放ogg,可以播放mp3, 音效无法播放ogg和mp3,可以播放wav

不过cocosdension如果一种格式不能播放会自动搜寻其他格式,所以背景音乐同目录下放了ogg和mp3两种格式,音效则放了ogg和wav格式,

不能播放问题就解决了,

播放延迟卡顿问题:

    如果你的音乐音效没有预加载,第一次播放音乐音效会有延迟,如果文件较大延迟会比较厉害,可以将资源路径添加到预加载目录,预加载避免延迟

    不过预加载会明显影响游戏加载速度,可能会导致画面卡住,如何取舍看个人了。

ios端浏览器第一次进场景加载音乐不能播放问题:

    原因是因为苹果的移动端浏览器不能自动播放音频,只能由用户的触摸(点击)事件触发加载,进游戏时音乐没有播放,苹果定的规则只能遵守了

    我的办法是在第一个场景做下判断,如果是手机端的ios浏览器,创建一个顶层的Layer,并添加事件,在触发onTouchBegan时playMusic就行了。

if(cc.sys.isMobile && cc.sys.os === cc.sys.OS_IOS)        {            var musicStartLayer = new cc.Layer();            musicStartLayer.x = 0;            musicStartLayer.y = 0;            this.addChild(musicStartLayer);            var listener = cc.EventListener.create({                event: cc.EventListener.TOUCH_ONE_BY_ONE,                swallowTouches: false,                onTouchBegan: function(touch, event){                    if(!self.isFirstTouch)                    {                        self.isFirstTouch = true;                        cc.audioEngine.playMusic("res/music.ogg");                    }                    return true;                }            });            cc.eventManager.addListener(listener, musicStartLayer);        }




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果4的id密码忘了怎么办 ipad己停用5分钟后再试怎么办 手机上大智慧日线失真怎么办 安卓手机突然地图信号弱怎么办 魔兽争霸对战模式没有金币要怎么办 常州号码被标记了商铺的名字怎么办 车提档了不接收怎么办公司能收回吗 稳岗补贴如果联系方式填错了怎么办 湖北驾照扣了50多分怎么办 南京驾照违章了50多分怎么办 有一个月没有去国税保税怎么办? 刑政复议通知书被邮政延误了怎么办 高考听力报名注册了两个用户怎么办 左腿神经损伤夏天脚冰凉怎么办 给区组织部的介绍信给到社区怎么办 被丈夫和儿子强送精神病院怎么办 练车穿短袖晒的胳膊特别黑怎么办 车子卖了对方迟迟不过户怎么办 成都华西医院就诊卡密码忘了怎么办 资阳办健康证怎么办要预约吗 头发出油厉害怎么办民间小偏方 你帮助别人别人却想着害你怎么办 怀孕接触有辐射的东西回怎么办 苹果手机用久了有点卡怎么办 4s店把我车撞了怎么办 长安之星2代大灯高不聚光怎么办 被电动车撞了人跑了怎么办 车被电动车撞了对方跑了怎么办 房子卖了户口没地方迁怎么办 酷派大神f2开不开机怎么办 酷派手机玩游戏竖屏怎么办 身上起红疙瘩很痒怎么办越挠越多 苹果6s指纹解锁坏了怎么办 案子结了网上追逃的怎么办 贷款买的手机不还了会怎么办 支付宝手机号没用了登陆不了怎么办 支付宝绑定的手机号注销了怎么办 考勤机进水了不能识别指纹怎么办? 网商银行人脸识别失败怎么办 电脑网页上的字变小了怎么办 把光驱换成固态硬盘后不识别怎么办