加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos creator基础-(九)cc.AudioSource使用

发布时间:2020-12-14 18:55:36 所属栏目:百科 来源:网络整理
导读:1: 掌握cc.AudioSource组件的使用; cc.AudioSource 1:AudioSource组件是音频源组件,发出声音的源头; 2: AudioSource组件面板: clip: 声源的播放的音频对象: AudioClip,mp3,wav,ogg, volume: 音量大小,[0,1]百分比 mute: 是否静音; Loop: 是否循环播放; Play

1: 掌握cc.AudioSource组件的使用;

cc.AudioSource


1:AudioSource组件是音频源组件,发出声音的源头;
2: AudioSource组件面板:
  clip: 声源的播放的音频对象: AudioClip,mp3,wav,ogg,
  volume: 音量大小,[0,1]百分比
  mute: 是否静音;
  Loop: 是否循环播放;
  Play on Load: 是否在组件加载的时候播放;
  Preload: 是否预先加载;

?

cc.AudioClip对象

1: 音频剪辑对象,支持的格式有mp3,ogg
2: 可以在编辑器上手动关联,生成AudioCip对象;
3: 可以通过代码加载AudioCip; (资源加载详细讲解);

?

AudioSource代码使用

1: 代码中获得cc.AudioSource组件:
  编辑器关联;
  代码获取组件;
2: AudioSource 主要的方法:
  play(); 播放音频;
  stop(); 停止声音播放;
  pause(); 暂停声音播放;
  resume(); 恢复声音播放;
  rewind(); 重头开始播放;
  其它接口见文档;
3: AudioSource代码主要属性:
  loop: 是否循环播放
  isPlaying: 是否正在播放;
  mute: 是否静音;
  如果要在开始的时候设置某些属性,可以放到start函数里面;

cc.Class({
    extends: cc.Component,properties: {
        // foo: {
        //    default: null,// The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,// optional,default is typeof default
        //    serializable: true,default is true
        //    visible: true,default is true
        //    displayName: ‘Foo‘,// optional
        //    readonly: false,default is false
        // },
        // ...
        // 编辑器来指定
        audio: {
            type: cc.AudioSource,default: null,},// use this for initialization
    onLoad: function () {
        // 获得节点,获得节点上的组件
        this.audio2 = this.node.getChildByName("audio").getComponent(cc.AudioSource);
    },start: function() {
        this.audio2.loop = true; // 循环播放,注意一下位置
        this.audio2.mute = false; // 设置静音
        console.log(this.audio2.isPlaying); // 是否正在播放
        // this.audio.play();
        this.audio2.play();

        /*this.scheduleOnce(function() {
            this.audio.stop();
        }.bind(this),3);*/

        /*this.scheduleOnce(function() {
            this.audio.pause(); // 暂停
        }.bind(this),3);
        
        this.scheduleOnce(function() {
            this.audio.resume(); // 恢复
        }.bind(this),6);*/
    },// called every frame,uncomment this function to activate update callback
    // update: function (dt) {

    // },
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读