【cocos creator】【TS】音乐播放器,拖拽音乐进度,暂停,继续,循环播放

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.AudioSource)
    music: cc.AudioSource = null;
    @property(cc.Slider)
    Slider: cc.Slider = null;
    @property(cc.Label)
    timeLabel: cc.Label = null;
    @property(cc.Sprite)
    pushBtn: cc.Sprite = null;
    @property(cc.Integer)
    playOverState: number = 0;

    playState: boolean = false;
    AllTime: number = 0;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
    }

    start() {
        //读取音乐时长
        this.AllTime = this.music.getDuration();
        this.music.play();
        this.setPlayState(false);
    }

    //滑动回调
    slider() {
        this.music.setCurrentTime(this.Slider.progress * this.AllTime);
    }

    update(dt) {
        this.setPlayProgress();
    }

    //根据音乐播放设置进度条的进度
    setPlayProgress() {
        if (!this.AllTime) return;
        let nowPlayTime = this.music.getCurrentTime();
        let allTime = this.AllTime;
        this.Slider.progress = nowPlayTime / allTime;
        //cc.log(this.music.getCurrentTime());
        this.timeLabel.string = this.setTimeLabel(nowPlayTime, allTime);

        //cc.log(nowPlayTime, this.AllTime);
        //音乐播放完
        if (nowPlayTime >= this.AllTime - 0.02) {
            this.PlayOverBack();
        }
    }

    //播放完后回调
    PlayOverBack() {
        if (this.playOverState === 1) {
            this.music.loop = false;
            this.setPlayState(false);
        }
        else if (this.playOverState === 0) {
            this.music.loop = true;
        }
    }

    //设置显示进度时间
    setTimeLabel(nowPlayTime, allTime) {
        let nowPlayTime1 = this.getClockTime(nowPlayTime);
        let allTime1 = this.getClockTime(allTime);
        return nowPlayTime1 + "/" + allTime1;
    }


    push() {
        if (this.playState) {
            this.playState = false;
        }
        else {
            this.playState = true;
        }
        this.setPlayState(this.playState);
    }

    setPlayState(play) {
        if (play) {
            if (this.music.getCurrentTime() === 0) {
                this.music.play();
            }
            else {
                this.music.resume();
            }
            this.loadPic("1", (res) => {
                this.pushBtn.spriteFrame = res;
            })
        }
        else {
            this.music.pause();
            this.loadPic("2", (res) => {
                this.pushBtn.spriteFrame = res;
            })
        }
    }

    //加载resources文件夹下的图片
    loadPic(url: string, cb: (res: cc.SpriteFrame) => void) {
        cc.loader.loadRes(url, cc.SpriteFrame, (err, spriteFrame) => {
            if (err) {
                cc.log(err);
                return cb(null);
            }
            cb(spriteFrame);
        })
    }
    //加载resources/music文件夹下的音乐
    loadMusic(url: string, cb: (res: cc.SpriteFrame) => void) {
        cc.loader.loadRes(url, cc.SpriteFrame, (err, spriteFrame) => {
            if (err) {
                cc.log(err);
                return cb(null);
            }
            cb(spriteFrame);
        })
    }

    //输入秒数转为时间显示 80=>1:20
    getClockTime(time) {
        let str = "";
        if (Math.floor(time / 3600) > 0) {
            str += Math.floor(time / 3600) + ":";
        }
        if (Math.floor(time % 3600 / 60) < 10) {
            str += "0" + Math.floor(time % 3600 / 60) + ":";
        }
        else {
            str += Math.floor(time % 3600 / 60) + ":";
        }
        if (Math.floor(time % 60) > 0) {
            if (Math.floor(time % 60) < 10) {
                str += "0" + Math.floor(time % 60);
            }
            else {
                str += Math.floor(time % 60);
            }
        }
        else {
            str += "00";
        }
        return str;
    }
}

在这里插入图片描述
在这里插入图片描述

Cocos Creator 中,你可以通过设置一个常驻节点来实现音乐播放器,并在切换场景后保持音乐播放。以下是具体的步骤: 1. 首先,在场景中创建一个空节点,作为音乐播放器的父节点。你可以将其命名为 "MusicManager" 或其他合适的名称。 2. 将音乐文件添加到项目资源中。你可以将音乐文件拖放到资源管理器中,或者通过代码动态加载。 3. 创建一个脚本组件,用于管理音乐播放。你可以将其附加到 "MusicManager" 节点上。 4. 在脚本组件中,使用 cc.AudioSource 组件来控制音乐播放。你可以通过以下代码示例来实现: ```javascript cc.Class({ extends: cc.Component, properties: { musicClip: { default: null, type: cc.AudioClip } }, onLoad () { cc.game.addPersistRootNode(this.node); // 将节点设置为常驻节点 // 创建一个新的子节点,用于存放 cc.AudioSource 组件 const audioNode = new cc.Node('Music'); const audioSource = audioNode.addComponent(cc.AudioSource); audioSource.clip = this.musicClip; audioSource.loop = true; audioSource.play(); audioNode.parent = this.node; }, }); ``` 5. 确保在每个场景切换之前,你需要在新场景加载完成之前将 "MusicManager" 节点设置为不被销毁。你可以在场景切换脚本或场景管理器中实现这一点。 以上就是在 Cocos Creator 中设置常驻节点音乐播放器并在切换场景后保持音乐播放的方法。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值