10.AudioSouce组件使用详解

1. cc.AudioSource

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

 

2. cc.AudioClip对象

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

 

3. AudioSource代码使用

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

 

代码示例: game_scene.js

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        //2种方式获取 ①编辑器指定
        audio: {
            default: null,
            type: cc.AudioSource,
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        //②代码获取
        this.audio2 = this.node.getChildByName("audio").getComponent(cc.AudioSource);

    },



    start () {
        this.audio.play();
    },

    // update (dt) {},
});

工程截图:

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值