mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-04 03:36:22 +08:00
feat: totally destory artplayer after leaving page
This commit is contained in:
@@ -41,7 +41,7 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
const contentType = response.headers.get('Content-Type') || '';
|
const contentType = response.headers.get('Content-Type') || '';
|
||||||
// rewrite m3u8
|
// rewrite m3u8
|
||||||
if (contentType.toLowerCase().includes('mpegurl')) {
|
if (contentType.toLowerCase().includes('mpegurl') || contentType.toLowerCase().includes('octet-stream')) {
|
||||||
// 获取最终的响应URL(处理重定向后的URL)
|
// 获取最终的响应URL(处理重定向后的URL)
|
||||||
const finalUrl = response.url;
|
const finalUrl = response.url;
|
||||||
const m3u8Content = await response.text();
|
const m3u8Content = await response.text();
|
||||||
|
|||||||
@@ -413,14 +413,33 @@ function LivePageClient() {
|
|||||||
const cleanupPlayer = () => {
|
const cleanupPlayer = () => {
|
||||||
if (artPlayerRef.current) {
|
if (artPlayerRef.current) {
|
||||||
try {
|
try {
|
||||||
|
// 先暂停播放
|
||||||
|
if (artPlayerRef.current.video) {
|
||||||
|
artPlayerRef.current.video.pause();
|
||||||
|
artPlayerRef.current.video.src = '';
|
||||||
|
artPlayerRef.current.video.load();
|
||||||
|
}
|
||||||
|
|
||||||
// 销毁 HLS 实例
|
// 销毁 HLS 实例
|
||||||
if (artPlayerRef.current.video && artPlayerRef.current.video.hls) {
|
if (artPlayerRef.current.video && artPlayerRef.current.video.hls) {
|
||||||
artPlayerRef.current.video.hls.destroy();
|
artPlayerRef.current.video.hls.destroy();
|
||||||
|
artPlayerRef.current.video.hls = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 销毁 FLV 实例
|
||||||
if (artPlayerRef.current.video && artPlayerRef.current.video.flv) {
|
if (artPlayerRef.current.video && artPlayerRef.current.video.flv) {
|
||||||
artPlayerRef.current.video.flv.destroy();
|
artPlayerRef.current.video.flv.destroy();
|
||||||
|
artPlayerRef.current.video.flv = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 移除所有事件监听器
|
||||||
|
artPlayerRef.current.off('ready');
|
||||||
|
artPlayerRef.current.off('loadstart');
|
||||||
|
artPlayerRef.current.off('loadeddata');
|
||||||
|
artPlayerRef.current.off('canplay');
|
||||||
|
artPlayerRef.current.off('waiting');
|
||||||
|
artPlayerRef.current.off('error');
|
||||||
|
|
||||||
// 销毁 ArtPlayer 实例
|
// 销毁 ArtPlayer 实例
|
||||||
artPlayerRef.current.destroy();
|
artPlayerRef.current.destroy();
|
||||||
artPlayerRef.current = null;
|
artPlayerRef.current = null;
|
||||||
@@ -552,9 +571,16 @@ function LivePageClient() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理之前的 HLS 实例
|
||||||
if (video.hls) {
|
if (video.hls) {
|
||||||
video.hls.destroy();
|
try {
|
||||||
|
video.hls.destroy();
|
||||||
|
video.hls = null;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('清理 HLS 实例时出错:', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const hls = new Hls({
|
const hls = new Hls({
|
||||||
debug: false,
|
debug: false,
|
||||||
enableWorker: true,
|
enableWorker: true,
|
||||||
@@ -669,7 +695,7 @@ function LivePageClient() {
|
|||||||
autoMini: false,
|
autoMini: false,
|
||||||
screenshot: false,
|
screenshot: false,
|
||||||
setting: false,
|
setting: false,
|
||||||
loop: true,
|
loop: false,
|
||||||
flip: false,
|
flip: false,
|
||||||
playbackRate: false,
|
playbackRate: false,
|
||||||
aspectRatio: false,
|
aspectRatio: false,
|
||||||
@@ -749,6 +775,20 @@ function LivePageClient() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// 页面卸载时的额外清理
|
||||||
|
useEffect(() => {
|
||||||
|
const handleBeforeUnload = () => {
|
||||||
|
cleanupPlayer();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
cleanupPlayer();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// 全局快捷键处理
|
// 全局快捷键处理
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyboardShortcuts = (e: KeyboardEvent) => {
|
const handleKeyboardShortcuts = (e: KeyboardEvent) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user