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 + " 弹窗");
}
}
}
适合那种长时间游戏场景中停留的游戏