This commit is contained in:
2025-10-13 15:39:35 +08:00
parent f0069c2745
commit 4953b86bb3
27 changed files with 4771 additions and 2063 deletions
+92 -4
View File
@@ -75,6 +75,11 @@ export class PreloadUI extends Component {
private loginLabel: Label;
private desc: Label;
// 视频播放状态跟踪
private videoPlayed: boolean = false;
private videoPlayRetryCount: number = 0;
private maxVideoPlayRetry: number = 3;
start() {
//Utils.addInnerEL(InnerMsgCode.UI_ShowPrompt, this, this.resiveShowPrompt)
Utils.parseNode(this.node, this._nodeTab);
@@ -90,12 +95,24 @@ export class PreloadUI extends Component {
this.loginLabel = this._nodeTab.loginLabel.getComponent(Label);
this.desc = this._nodeTab.Desc.getComponent(Label);
// 设置视频为静音,提高移动端自动播放成功率
this.VideoPlayer.muted = true;
this.VideoPlayer.loop = true;
this.VideoPlayer.stayOnBottom = true;
this.VideoPlayer.node.on(
VideoPlayer.EventType.META_LOADED,
this.onVideoReadyToPlay,
this
);
// 监听播放失败事件
this.VideoPlayer.node.on(
VideoPlayer.EventType.PLAYING,
this.onVideoPlaying,
this
);
GButton.BandClick(this.loginBtn, this.onLogin, this);
this.jinduBar = this.node.getChildByName("jinduBar");
@@ -109,7 +126,8 @@ export class PreloadUI extends Component {
this.jinduFilled.fillRange = 0;
if (!this.VideoPlayer.isPlaying) this.VideoPlayer.play();
// 移除直接播放调用,等待 META_LOADED 事件后再播放
// if (!this.VideoPlayer.isPlaying) this.VideoPlayer.play();
//初始化显示
switch (LanguageUtils.CurrentLanguage) {
@@ -148,11 +166,74 @@ export class PreloadUI extends Component {
this.mainScene.init();
}
protected onDestroy(): void {}
protected onDestroy(): void {
// 清理视频事件监听器
if (this.VideoPlayer && this.VideoPlayer.node) {
this.VideoPlayer.node.off(
VideoPlayer.EventType.META_LOADED,
this.onVideoReadyToPlay,
this
);
this.VideoPlayer.node.off(
VideoPlayer.EventType.PLAYING,
this.onVideoPlaying,
this
);
this.VideoPlayer.stop();
}
}
onVideoReadyToPlay() {
logger.log("Video Ready");
this._nodeTab.VideoPlayer.getComponent(VideoPlayer).play();
logger.log("PreloadUI: Video metadata loaded, attempting to play");
this.tryPlayVideo();
}
/**
* 尝试播放视频,带重试机制和错误处理
*/
private tryPlayVideo() {
if (this.videoPlayed) {
logger.log("PreloadUI: Video already playing, skip");
return;
}
if (!this.VideoPlayer) {
logger.error("PreloadUI: VideoPlayer not initialized");
return;
}
// 尝试播放视频
try {
this.VideoPlayer.play();
logger.log(`PreloadUI: Video play attempted (retry: ${this.videoPlayRetryCount}/${this.maxVideoPlayRetry})`);
// 延迟检查播放状态,如果失败则重试
this.scheduleOnce(() => {
if (!this.VideoPlayer.isPlaying && this.videoPlayRetryCount < this.maxVideoPlayRetry) {
this.videoPlayRetryCount++;
logger.warn(`PreloadUI: Video play failed, retrying... (${this.videoPlayRetryCount}/${this.maxVideoPlayRetry})`);
this.tryPlayVideo();
} else if (!this.VideoPlayer.isPlaying) {
logger.warn("PreloadUI: Video autoplay failed after max retries. Will retry on user interaction.");
}
}, 0.5);
} catch (error) {
logger.error("PreloadUI: Video play error:", error);
if (this.videoPlayRetryCount < this.maxVideoPlayRetry) {
this.videoPlayRetryCount++;
this.scheduleOnce(() => {
this.tryPlayVideo();
}, 1.0);
}
}
}
/**
* 视频开始播放回调
*/
private onVideoPlaying() {
logger.log("PreloadUI: Video is now playing");
this.videoPlayed = true;
}
update(deltaTime: number) {
if (!this.preloadFinish) {
@@ -227,6 +308,13 @@ export class PreloadUI extends Component {
// }
async onLogin() {
// 用户交互时尝试播放视频(如果自动播放失败)
if (!this.videoPlayed && this.VideoPlayer) {
logger.log("PreloadUI: User interaction detected, trying to play video");
this.videoPlayRetryCount = 0; // 重置重试计数
this.tryPlayVideo();
}
const deviceId = DeviceIdService.I.id;
const os = MachineInfoService.I.getMachineOs();
const reqData = {