当前位置: 首页 > article >正文

cocos creator 3.8 抖音、字节跳动录制器 12

 

@property(Node)
luzhishijianDisplay: Node = null!;//录制时间显示
@property(Node)
luzhikaishiBut: Node = null!;//录制开始
@property(Node)
luzhijieshuBut: Node = null!;//录制结束
luzhikaishiType: boolean = false;//是否开始录制开始计时
gameluzhiTime: number = 0;

onLoad() {
    //@ts-ignore
    if (window.ks || window.tt) {
        this.luzhishijianDisplay.active = true;
        this.luzhikaishiBut.active = true;//录制开始
        this.luzhijieshuBut.active = false;//录制结束
    } else {
        this.luzhishijianDisplay.active = false;
        this.luzhikaishiBut.active = false;
        this.luzhijieshuBut.active = false;
    };
    this.luzhikaishiType = false;
    this.gameluzhiTime = 0;
}

update(dt: number) {
        if (this.luzhikaishiType) {
            this.gameluzhiTime += dt;
            let mlp: any = Math.floor(this.gameluzhiTime / 60);
            let slp: any = Math.floor(this.gameluzhiTime - mlp * 60);
            if (mlp < 10) { mlp = "0" + mlp };
            if (slp < 10) { slp = "0" + slp };
            ((this.luzhishijianDisplay as Node).getComponent(Label) as Label).string = mlp + ":" + slp;
            if (this.gameluzhiTime > 280) {
                this.onBtnFenxiangluping();
            }
        } else {
            ((this.luzhishijianDisplay as Node).getComponent(Label) as Label).string = "";
        };
}

//两个按钮挂的事件
//点击开始录屏
onBtnKaishiluping() {
    this.luzhikaishiBut.active = false;//录制开始
    this.luzhijieshuBut.active = true;//录制结束
    this.luzhikaishiType = true;
    this.gameluzhiTime = 0;
    ByteDanceSDK.getInstance().luzhiKaishi();
}
//点击分享录屏
onBtnFenxiangluping() {
    this.luzhikaishiBut.active = true;//录制开始
    this.luzhijieshuBut.active = false;//录制结束
    this.luzhikaishiType = false;
    if (this.gameluzhiTime < 11) {
        ByteDanceSDK.getInstance().showToast("录制时间过短");
        return;
    };
    ByteDanceSDK.getInstance().luzhijieshu();
}

@ccclass('ByteDanceSDK')
export class ByteDanceSDK {
    private static instance: ByteDanceSDK;
    public static getInstance() {
        if (!ByteDanceSDK.instance) {
            ByteDanceSDK.instance = new ByteDanceSDK();
        }
        return ByteDanceSDK.instance;
    }
    recorder: any = null!;
    luzhitime: number = 0;
    videoPath: string = "";
    //初始化 在合适的地方调用
    initByteDanceSdk() {
        //@ts-ignore
        if (window["tt"]) {
            this.luzhiInit();
            this.bundleShareActive();
        }
    }

    luzhiInit() {
        //@ts-ignore
        this.recorder = tt.getGameRecorderManager();
        this.recorder.onStart(() => {
            console.log('录屏开始');
            this.luzhitime = new Date().getTime();
        });
        this.recorder.onStop((res: any) => {
            this.videoPath = res.videoPath;
            console.log('录屏结束:' + res.videoPath);
        });
        this.recorder.onPause(() => {
            console.log('录屏暂停');
        });
        this.recorder.onResume(() => {
            console.log('录屏继续');
        });
    }

    luzhiKaishi() {
        //@ts-ignore
        if (window["tt"]) {
            if (!this.recorder) {
                //@ts-ignore
                this.recorder = tt.getGameRecorderManager();
            }
            this.recorder.stop();
            //录屏的最长时间是五分钟
            this.recorder.start({
                duration: 300
            });
        }
    }

    luzhijieshu() {
        //@ts-ignore
        if (window["tt"]) {
            //小于十秒就是录制失败,由于帧数的不稳定,这里使用15秒
            if (new Date().getTime() - this.luzhitime <= 15000) {
                // //@ts-ignore
                // tt.showToast({
                //     title: '录频异常时间过短',
                //     mask: true,
                //     duration: 1000,
                // });
                this.videoPath = "";
                return;
            }
            if (!this.recorder) {
                //@ts-ignore
                this.recorder = tt.getGameRecorderManager();
            }
            this.recorder.stop();
            setTimeout(() => {
                if (this.videoPath != "") {
                    this.fenxiangVideo();
                }
            }, 500);
        }
    }

    bundleShareActive() {
        //@ts-ignore
        if (window["tt"]) {
            //@ts-ignore
            tt.onShareAppMessage(function (res: any) {
                return {
                    success() {
                        // console.log("分享成功");
                    },
                    fail(e: any) {
                        // console.log("分享失败", e);
                    },
                };
            });
        }
    }

    // 对比版本号
    compareVersion(v1: any, v2: any) {
        v1 = v1.split('.');
        v2 = v2.split('.');
        const len = Math.max(v1.length, v2.length);
        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        for (let i = 0; i < len; i++) {
            const num1 = parseInt(v1[i]);
            const num2 = parseInt(v2[i]);
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
        return 0;
    }

    fenxiangVideo() {
        console.log("*************调用分享游戏视频*************");
        //@ts-ignore
        if (window["tt"]) {
            //@ts-ignore
            tt.shareAppMessage({
                channel: 'video',
                title: "这小游戏真好玩",
                extra: {
                    videoPath: this.videoPath, // 可用录屏得到的视频地址
                    videoTopics: ["跟我一起来玩这款小游戏吧"],
                    hashtag_list: ["跟我一起来玩这款小游戏吧"]
                },
                success() {
                    console.log('分享视频成功');
                    this.videoPath = "";
                },
                fail(e: any) {
                    console.log('分享视频失败' + JSON.stringify(e));
                }
            });
        }
    }

    showToast(message: string) {
        if (sys.Platform.BYTEDANCE_MINI_GAME) {
            //@ts-ignore
            tt.showToast({
                title: message,
                icon: "none",
                duration: 1000,
            });
        } else {
            console.log(message + " 弹窗");
        }
    }
}

适合那种长时间游戏场景中停留的游戏


http://www.kler.cn/a/420603.html

相关文章:

  • 组合数学——鸽巢原理
  • Lumos学习王佩丰Excel第十九讲:Indirect函数
  • 记录一次网关异常
  • JavaScript根据数据生成柱形图
  • 006 MATLAB编程基础
  • 前端通用Axios 请求拦截配置
  • 40 基于单片机的温湿度检测判断系统
  • 3D Bounce Ball Game 有什么技巧吗?
  • Linux笔记---进程:进程地址空间
  • 软件质量保证——单元测试之黑盒技术
  • 做异端中的异端 -- Emacs裸奔之路4: 你不需要IDE
  • FPGA 开发工程师
  • Linux firewalld常用命令
  • XML 查看器:深入理解与高效使用
  • 记录学习《手动学习深度学习》这本书的笔记(二)
  • Oracle ASM存储学习和相关视图
  • 基于“开源 2+1 链动 O2O 商城小程序”的门店拉新策略与流程设计
  • 基于单片机的四位数码管检测有毒气体
  • 基于Java Springboot个人财务APP且微信小程序
  • 【Spring源码核心篇-06】spring中事务的底层实现与执行流程
  • vue.js学习(day 13)
  • C基础练习题
  • Ubuntu22.04上kdump和crash的使用
  • D83【python 接口自动化学习】- pytest基础用法
  • 一键生成数据库对应的所有DataX的json文件
  • mvc基础及搭建一个静态网站