You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

238 lines
7.6 KiB

import { sys, _decorator } from "cc";
import { TTHarmonyCompat } from "./TTHarmonyCompat";
const IGG_URL: string = "https://gate.snssdk.com/developer/api/get_top_video_ids_by_like"
const NEW_URL: string = "https://gate.snssdk.com/developer/api/get_top_video_ids_by_time"
const { ccclass, property } = _decorator;
/**
* 头条录屏相关
*/
@ccclass
export default class TTRecording {
static Ins: TTRecording = new TTRecording();
private _recorder: any
private _videoPath: string = ""
private _iTime: number = 0
private _timeOutId
private _isStop: boolean = true
private _appid = ``;
private _game_name = ``;
getVideoPath() {
return this._videoPath;
}
getVideoTime() {
return this._iTime;
}
getStatus() {
return !this._isStop;
}
init(data) {
if (data && data.appid) this._appid = data.appid;
if (data && data.gameName) this._game_name = data.gameName;
if (this._recorder == null) {
let self = this;
this._recorder = window["tt"].getGameRecorderManager()
this._recorder.onStart(res => {
// console.log('录屏开始')
self._isStop = false
self._videoPath = null
self._iTime = 0
clearInterval(self._timeOutId)
self._timeOutId = setInterval(() => {
!self._isStop && self._iTime++
}, 1000)
})
this._recorder.onStop((res) => {
self._isStop = true
let time = self._iTime >= 30 ? 30 : self._iTime
if (time < 3) return
// console.log(time + "onStop=", res, time)
// self._iTime = 0
self._recorder.clipVideo({
path: res.videoPath,
timeRange: [time, 0],
success(res) {
// console.log(time + res.videoPath)
self._videoPath = res.videoPath
},
fail(e) {
console.error(String(e))
}
})
})
}
}
startVideo() {
if (this._recorder) {
this._recorder.stop();
this._recorder.start({
duration: 300,
});
this._isStop = false;
}
}
pauseVideo() {
this._recorder && this._recorder.pause();
}
resumeVideo() {
this._recorder && this._recorder.resume()
if (this._isStop)
this.startVideo();
}
stopVideo() {
if (this._recorder && !this._isStop) {
this._recorder.stop()
this._isStop = true
}
}
shareVideo(successFunc: Function) {
this._timeOutId > -1 && clearInterval(this._timeOutId)
let path = this._videoPath;
if (this._iTime < 3) {
// console.log("录屏失败:录屏时长低于 3 秒");
} else if (this._videoPath) {
// console.log("shareVideo 分享开始", this._iTime)
let self = this
self._videoPath = null;
const ok = TTHarmonyCompat.shareAppMessage({
channel: 'video',
extra: {
videoPath: path,
videoTopics: [this._game_name],
withVideoId: true,
},
success() {
successFunc && successFunc(true)
// console.log('分享视频成功')
},
fail(e) {
successFunc && successFunc(false)
// console.log('分享视频失败', e.errMsg)
if (e.errMsg.indexOf("short") != -1) {
// console.log("分享视频失败,视频时间过短")
window["tt"].showToast({ title: "video time too short", icon: "fail" });
}
}
});
if (!ok) {
successFunc && successFunc(false);
// console.log('鸿蒙环境暂不支持录屏分享');
}
} else {
successFunc && successFunc(false)
// console.log("shareVideo 分享失败", path, this._iTime)
}
}
//获取视频点赞排行
getVideoDiggRank(token: string, cb: Function) {
this.requestVideoRank(token, IGG_URL, cb)
}
//获取视频最新排行
getVideoNewRank(token: string, cb: Function) {
this.requestVideoRank(token, NEW_URL, cb)
}
//请求视频排行数据
requestVideoRank(token: string = "", url: string = "", cb: Function) {
window["tt"].request({
url: url,
method: "POST",
data: {
app_id: this._appid,
number_of_top: 100,
//tag: "rank", //分享视频时传入videoTag情况下,用于筛选指定tag视频
access_token: token,
},
success: (res) => {
// 从res中获取所需视频信息(videoId数组索引与返回数据数组索引一一对应)
if (res.data.err_no !== 0) {
cb && cb(null)
// console.log("视频排行数据获取失败=====")
} else {
// console.log(res)
cb && cb(res.data.data)
}
},
})
}
//排行视频点击跳转
navigateToVideo(videoId: number) {
const ok = TTHarmonyCompat.navigateToVideoView({
videoId: videoId,
success: (res) => {
// console.log("done")
},
fail: (err) => {
if (err.errCode === 1006) {
window["tt"].showToast({
title: "something wrong with your network",
})
}
},
});
if (!ok) {
// console.log('鸿蒙环境暂不支持跳转视频播放页');
}
}
request(url: string = "", data: any = null, method: string = "", success: Function = null, fail: Function = null, complete: Function = null) {
if (sys.platform == sys.Platform.DESKTOP_BROWSER) return
let xhr = new XMLHttpRequest()
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let response = xhr.responseText
if (xhr.status >= 200 && xhr.status < 400) {
let result = {}
try {
result = JSON.parse(response)
} catch (e) {
console.error('json parse error ', response)
fail && fail(e)
}
success && success(result)
} else {
console.error('error ', response)
fail && fail(response)
}
} else { }
}
xhr.timeout = 3000
xhr.ontimeout = function (event) {
console.error('error ')
fail && fail(event)
}
xhr.open(method, url, true)
if (method == "POST") {
var object2Query = function (obj) {
var args = []
for (var k in obj)
args.push(k + "=" + obj[k])
return args.join("&") // 返回对象
}
xhr.open('POST', url)
xhr.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded')
xhr.send(object2Query(data))
} else {
xhr.send()
}
}
}