export enum EVideoEvent { popError = 'popError', // 短视频浮层失败 popClose = 'popClose', // 短视频浮层关闭 canplay = 'canplay', canplaythrough = 'canplaythrough', finish = 'finish', error = 'error', playing = 'playing', pause = 'pause', stalled = 'stalled', timeupdate = 'timeupdate', ended = 'ended', click = 'click', firstvideoframe = 'firstvideoframe', firstvideoframeinrender = 'firstvideoframeinrender', }
标准参考:https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-erroropen in new window:
1 = MEDIA_ERR_ABORTED - 取回过程被用户中止
2 = MEDIA_ERR_NETWORK - 当下载时发生错误
3 = MEDIA_ERR_DECODE - 当解码时发生错误
4 = MEDIA_ERR_SRC_NOT_SUPPORTED - 不支持视频
const resizeImage = (originalWidth, originalHeight, containerWidth, containerHeight) => { const scaleWidth = containerWidth / originalWidth; const scaleHeight = containerHeight / originalHeight; const scale = Math.min(scaleWidth, scaleHeight); const width = originalWidth * scale; const height = originalHeight * scale; return { width, height }; } const plugin = my.tb.getInteractiveSDK(); let videoPopController; try { videoPopController = plugin.getVideoPopController(); } catch (e) { console.error(e); error('getVideoPopController init error',e.toString()); } const { platform, windowWidth, windowHeight, screenWidth, screenHeight } = my.getSystemInfoSync(); const platformLower = platform?.toLowerCase?.(); // 需要手动设置视频尺寸 const videoSize = { width: 1080, height: 1920 }; const scaleSize = resizeImage(videoSize.width, videoSize.height, windowWidth || screenWidth, windowHeight || screenHeight); // 因为iOS上尺寸计算有问题,需要开发者自行计算尺寸。 await videoPopController.open({ width: platformLower === 'ios' ? scaleSize.width : undefined, height: platformLower === 'ios' ? scaleSize.height : undefined, url: 'https://xxxx.mp4', timeout: 5000, });
如果你游戏横屏的,建议开启沉浸式模式,不然获取到的屏幕尺寸偏小,视频最后展示出来也是偏小。
{ "window":{ "transparentTitle": "always" } }
加载失败情况:
目前sdk针对持续处于stalled状态5秒会提示报错,会提示用户关闭浮层后重新进入;开发者可以通过设置timeout参数来设置等待时间,单位为毫秒;
try { await videoPopController.close(); } catch (hideVideoPopError) { console.log(hideVideoPopError); }
videoPopController.onEvent((data) => { console.log('onVideoEvent',data); if(data?.event === 'popClose'){ videoPopController.offEvent(); console.warn('短视频浮层关闭了') }else if(data?.event === 'error'){ console.warn('短视频播放报错了') } });
videoPopController.offEvent();
目前浮层无法在IDE上打开,建议直接使用真机调试和开发;
因为属于SDK新功能,客户端会缓存老的SDK,不一定能执行到最新SDK的版本。建议开发者删掉手淘后重新再试;也建议开发去判断是否能拿到此Controller对象,如果拿不到此对象,给与用户一定的提示。