修改视频播放器处理逻辑

master
于肖磊 2026-02-11 10:48:06 +08:00
parent 80a20f365d
commit 908e10f20a
1 changed files with 210 additions and 88 deletions

View File

@ -203,7 +203,9 @@
header_padding_left: '',
report_type_list: [],
direction: 'direction',
};
video_switch_debounce_timer: null, //
video_cleanup_timer: null, //
}
},
computed: {
swiperStyle() {
@ -237,6 +239,42 @@
onShow() {
this.init();
},
beforeDestroy() {
//
if (this.video_switch_debounce_timer) {
clearTimeout(this.video_switch_debounce_timer);
}
if (this.video_cleanup_timer) {
clearTimeout(this.video_cleanup_timer);
}
//
this.cleanup_all_videos();
//
// #ifdef H5
if (typeof document !== 'undefined') {
document.removeEventListener('keydown', this.handle_keydown);
}
// #endif
},
// activated deactivated
activated() {
//
if (!this.paused && this.current_index >= 0) {
setTimeout(() => {
this.play_current_video_safely(this.current_index);
}, 100);
}
},
deactivated() {
//
this.pause_all_videos_except(-1); // -1
this.setData({ paused: true });
},
methods: {
isEmpty,
init() {
@ -376,23 +414,142 @@
}
});
},
video_play_event(video_contexts, is_first_play = false) {
try {
if (is_first_play) {
video_contexts.play().catch(() => {
this.setData({
paused: true,
});
});
} else {
video_contexts.play();
}
} catch (error) {
this.setData({
paused: true,
//
handle_swiper_change(event) {
const { current } = event.detail;
//
if (this.video_switch_debounce_timer) {
clearTimeout(this.video_switch_debounce_timer);
}
this.video_switch_debounce_timer = setTimeout(() => {
this.process_swiper_change(current);
}, 50); // 50ms
},
// swiper
process_swiper_change(current) {
const previousIndex = this.current_index;
//
this.pause_all_videos_except(current);
//
this.setData({
current_index: current,
paused: false,
current_video_progress: 0,
current_video_duration: 0,
is_seeking: false,
});
const id = this.display_video_list[current].id;
const index = this.video_data_list.findIndex(item => item.id == id);
//
if (index < 2 && this.direction == 'prev') {
this.get_last_or_next_data_list(this.video_data_list[0].id, 1, 0);
} else if (index < this.video_data_list.length - 3 && this.direction == 'next') {
this.get_last_or_next_data_list(this.video_data_list[this.video_data_list.length - 1].id, 0, 1);
}
// ID
this.current_video_id = this.display_video_list[current].id;
//
if (this.current_video_index == 0 && this.is_slide_start) {
const list = [
this.get_video_by_index(0),
this.get_video_by_index(1),
this.get_video_by_index(2)
];
this.setData({
is_slide_start: false,
current_index: 0,
display_video_list: list,
swiper_key: get_math()
});
} else if (this.current_video_index == this.video_data_list.length - 1) {
const list = [
this.get_video_by_index(this.current_video_index - 2),
this.get_video_by_index(this.current_video_index - 1),
this.get_video_by_index(this.current_video_index),
];
this.setData({
current_index: 2,
display_video_list: list,
swiper_key: get_math()
});
} else {
this.setData({
is_slide_start: true,
swiper_key: get_math()
});
this.update_display_data();
}
//
this.update_share_info(this.display_video_list[current]);
// DOM
setTimeout(() => {
this.play_current_video_safely(current);
}, 150);
},
//
pause_all_videos_except(exceptIndex) {
// uni.createVideoContext
this.create_video_contexts.forEach((context, index) => {
if (index !== exceptIndex && context) {
try {
context.pause();
} catch (error) {
console.warn(`暂停视频 ${index} 失败:`, error);
}
}
});
},
//
play_current_video_safely(index) {
// 使 uni.createVideoContext
if (this.create_video_contexts[index]) {
this.video_play_event(this.create_video_contexts[index], false);
return;
}
},
//
toggle_play_pause() {
const currentIndex = this.current_index;
//
const videoContext = this.create_video_contexts[currentIndex] || this.video_contexts[currentIndex];
if (!videoContext) {
console.warn(`当前索引 ${currentIndex} 无可用视频上下文`);
return;
}
this.setData({
paused: !this.paused
});
if (this.paused) {
//
try {
videoContext.pause();
} catch (error) {
console.warn('暂停视频失败:', error);
}
} else {
//
this.video_play_event(videoContext, false);
}
},
update_share_info(data) {
const info = {
title: data.title || '',
@ -407,6 +564,26 @@
//
app.globalData.page_share_handle(info);
},
//
video_play_event(videoContext, is_first_play = false) {
if (!videoContext) {
this.setData({ paused: true });
return;
}
try {
if (is_first_play) {
videoContext.play().catch((error) => {
this.setData({ paused: true });
});
} else {
videoContext.play();
}
} catch (error) {
console.error('视频播放异常:', error);
this.setData({ paused: true });
}
},
//
get_video_by_index(index) {
//
@ -559,78 +736,6 @@
})
}
},
//
handle_swiper_change(event) {
const { current } = event.detail;
const previousIndex = this.current_index;
if (this.create_video_contexts[previousIndex]) {
this.create_video_contexts[previousIndex].pause();
}
this.setData({
current_index: current,
paused: false,
current_video_progress: 0,
current_video_duration: 0,
is_seeking: false,
})
const id = this.display_video_list[current].id;
const index = this.video_data_list.findIndex(item => item.id == id);
//
if (index < 2 && this.direction == 'prev') {
this.get_last_or_next_data_list(this.video_data_list[0].id, 1, 0);
} else if (index < this.video_data_list.length - 3 && this.direction == 'next') {
//
this.get_last_or_next_data_list(this.video_data_list[this.video_data_list.length - 1].id, 0, 1);
}
// ID
this.current_video_id = this.display_video_list[current].id;
//
if (this.current_video_index == 0 && this.is_slide_start) {
// this.$nextTick(() => {
const list = [
this.get_video_by_index(0),
this.get_video_by_index(1),
this.get_video_by_index(2)
];
this.setData({
is_slide_start: false,
current_index: 0,
display_video_list: list,
swiper_key: get_math()
})
// })
} else if (this.current_video_index == this.video_data_list.length - 1) {
// this.$nextTick(() => {
const list = [
this.get_video_by_index(this.current_video_index - 2),
this.get_video_by_index(this.current_video_index - 1),
this.get_video_by_index(this.current_video_index),
];
this.setData({
current_index: 2,
display_video_list: list,
swiper_key: get_math()
})
} else {
this.setData({
is_slide_start: true
})
// index
this.update_display_data();
}
//
this.update_share_info(this.display_video_list[current]);
setTimeout(() => {
//
if (this.create_video_contexts[current]) {
this.video_play_event(this.create_video_contexts[current], false);
}
}, 100);
},
//
toggle_play_pause() {
if (this.create_video_contexts[this.current_index] == null) return; // 1
@ -893,7 +998,24 @@
//
product_close_event() {
console.log('121245');
}
},
//
cleanup_all_videos() {
try {
//
this.pause_all_videos_except(-1);
//
this.create_video_contexts = [];
this.video_contexts = [];
console.log('视频资源清理完成');
} catch (error) {
console.error('清理视频资源时出错:', error);
}
},
}
};
</script>