Cocos Creator实战教程(13)】——播放声音

1. 相关知识点html

1.1 使用 AudioSource 组件播放编辑器

建立一个空节点,在这个空节点上,添加一个 其余组件 -> AudioSourceoop

在脚本上预设好 AudioSource,而且根据实际需求,完善脚本的对外接口,以下:this

cc.Class({url

properties: {htm

audioSource: {对象

type: cc.AudioSource,接口

default: nullip

},资源

},

play: function () {

this.audioSource.play();

},

pause: function () {

this.audioSource.pause();

},

});

1.2 使用 AudioEngine 播放

在脚本内定义一个 audioClip 资源对象,以下示例中 properties 对象内。

直接使用 cc.audioEngine.play(audio, loop, volume); 播放。以下示例中 onLoad 中。

cc.Class({

properties: {

audio: {

default: null,

type: cc.AudioClip

}

},

onLoad: function () {

this.current = cc.audioEngine.play(this.audio, false, 1);

},

onDestroy: function () {

cc.audioEngine.stop(this.current);

}

});

AudioEngine 播放的时候,须要注意这里的传入的是一个完整的 AudioClip 对象(而不是 url)。 因此咱们不建议在 play 接口内直接填写音频的 url 地址,而是但愿你们先定义一个 AudioClip,而后在编辑器内将音频拖拽过来。


参考文档和完整的文档和源码下载地址:

https://www.write-bug.com/article/1846.html

相关文章
相关标签/搜索