uniapp:video无法终断网络连接
一个非常简单的功能使用video标签播放视频点下一页播放下一个但是从控制台发现上一个视频的网络拉流依旧保持无法被终断多翻几页的话带宽就全被占住了。即便是使用v-if、stop、pause、src赋空值等各种手段销毁了video实例网络连接依旧断不掉阴魂不散。就在我要绝望的时候我决定换一个AI来问一下果然它告诉由于小程序的 video 是原生组件需通过上下文 API 强制销毁实例。要给video加id。template video classvideo-player :idvideoId :srcvideoUrl autoplay muted playsinline object-fitfill errorhandleError loadeddatahandleLoadedData playhandlePlay canplayhandleCanPlay /video /templateconst instance getCurrentInstance(); const videoId video_${Date.now()}_${Math.random().toString(36).slice(2, 8)}; // 获取视频上下文 const getVideoContext () { if (!videoContext) { videoContext uni.createVideoContext(videoId, instance?.proxy); } return videoContext; }; const destroyVideo () { return new Promise((resolve) { try { isDestroyed true; loadSuccessFlag false; if (retryTimer.value) { clearTimeout(retryTimer.value); retryTimer.value null; } const ctx getVideoContext(); ctx.pause(); ctx.stop(); ctx.exitFullScreen(); // 延迟清空 src让 stop() 有足够时间生效 videoUrl.value ; retryCount.value 0; isLoading.value false; resolve(); } catch (e) { console.warn(视频销毁失败:, e); resolve(); } }); };