videoUtils.js 749 B

12345678910111213141516171819202122232425
  1. const videoContextCache = {}; // 使用普通对象作为缓存池
  2. /**
  3. * 获取缓存的 videoContext,如果没有则创建并缓存
  4. * @param {string} videoId 视频组件的 id
  5. * @returns {object} videoContext 对象
  6. */
  7. export function getVideoContext(videoId,currentComponent) {
  8. if (!videoContextCache[videoId]) {
  9. // 如果未缓存,则创建并缓存
  10. videoContextCache[videoId] = uni.createVideoContext(videoId,currentComponent);
  11. }
  12. return videoContextCache[videoId];
  13. }
  14. /**
  15. * 清理缓存的 videoContext
  16. * @param {string} videoId 视频的唯一标识符
  17. */
  18. export function clearCachedVideoContext(videoId) {
  19. if (videoContextCache[videoId]) {
  20. delete videoContextCache[videoId]; // 从 JSON 对象中删除
  21. }
  22. }