Commit 0d5b7a31 authored by liujiangnan's avatar liujiangnan

feat: 联动播放

parent 1e5ec877
...@@ -309,6 +309,7 @@ html, body { ...@@ -309,6 +309,7 @@ html, body {
.wavesurfer-region { .wavesurfer-region {
z-index: 2 !important; z-index: 2 !important;
pointer-events: all !important; pointer-events: all !important;
cursor: pointer !important;
} }
/* 页面区间样式 */ /* 页面区间样式 */
...@@ -317,11 +318,13 @@ html, body { ...@@ -317,11 +318,13 @@ html, body {
background: rgba(255, 192, 203, 0.3) !important; background: rgba(255, 192, 203, 0.3) !important;
border: 2px solid rgba(255, 192, 203, 0.8) !important; border: 2px solid rgba(255, 192, 203, 0.8) !important;
pointer-events: all !important; pointer-events: all !important;
cursor: pointer !important;
} }
/* 组区间样式 */ /* 组区间样式 */
.wavesurfer-region:not([data-id^="page-"]) { .wavesurfer-region:not([data-id^="page-"]) {
z-index: 1 !important; z-index: 1 !important;
cursor: pointer !important;
} }
.wavesurfer-handle { .wavesurfer-handle {
......
...@@ -41,6 +41,10 @@ new Vue({ ...@@ -41,6 +41,10 @@ new Vue({
// 图片预加载相关 // 图片预加载相关
preloadedImages: {}, preloadedImages: {},
preloadProgress: 0, preloadProgress: 0,
// 新增:播放控制相关
playTimer: null,
currentPlayingRegion: null,
}, },
mounted() { mounted() {
...@@ -260,19 +264,25 @@ new Vue({ ...@@ -260,19 +264,25 @@ new Vue({
const pageRanges = []; const pageRanges = [];
this.pages.forEach((page, pageIndex) => { this.pages.forEach((page, pageIndex) => {
if (page.groups && page.groups.length > 0) { if (page.groups && page.groups.length > 0) {
const times = page.groups.reduce((acc, group) => { // 找出所有有效的时间点
const validTimes = page.groups.reduce((acc, group) => {
// 确保时间值是有效的数字且精确到小数点后3位
if (typeof group.start === 'number' && !isNaN(group.start)) { if (typeof group.start === 'number' && !isNaN(group.start)) {
acc.starts.push(group.start); acc.starts.push(Number(group.start.toFixed(3)));
} }
if (typeof group.end === 'number' && !isNaN(group.end)) { if (typeof group.end === 'number' && !isNaN(group.end)) {
acc.ends.push(group.end); acc.ends.push(Number(group.end.toFixed(3)));
} }
return acc; return acc;
}, { starts: [], ends: [] }); }, { starts: [], ends: [] });
if (times.starts.length > 0 && times.ends.length > 0) { if (validTimes.starts.length > 0 && validTimes.ends.length > 0) {
const start = Math.min(...times.starts); // 使用精确的最小和最大值
const end = Math.max(...times.ends); const start = Math.min(...validTimes.starts);
const end = Math.max(...validTimes.ends);
// 确保开始时间小于结束时间
if (start < end) {
pageRanges.push({ pageRanges.push({
pageIndex, pageIndex,
start, start,
...@@ -280,6 +290,7 @@ new Vue({ ...@@ -280,6 +290,7 @@ new Vue({
}); });
} }
} }
}
}); });
// 按开始时间排序 // 按开始时间排序
...@@ -305,71 +316,52 @@ new Vue({ ...@@ -305,71 +316,52 @@ new Vue({
if (region) { if (region) {
this.audioRegions[pageRegionId] = region; this.audioRegions[pageRegionId] = region;
// 添加点击事件 // 添加点击事件处理
region.element.addEventListener('click', (event) => { const handleClick = (event) => {
// 阻止事件冒泡 // 阻止事件冒泡
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
console.log('Page region clicked:', {
pageIndex: range.pageIndex,
pageRegionId,
currentPage: this.currentPageIndex,
timeRange: { start: range.start, end: range.end }
});
// 切换到对应的页面 // 切换到对应的页面
this.currentPageIndex = range.pageIndex + 1; this.currentPageIndex = range.pageIndex + 1;
this.currentPage = this.pages[range.pageIndex]; this.currentPage = this.pages[range.pageIndex];
console.log('Page switched to:', {
newPageIndex: this.currentPageIndex,
newPage: this.currentPage
});
// 取消当前选中的组 // 取消当前选中的组
this.selectedGroup = null; this.selectedGroup = null;
this.currentGroup = null; this.currentGroup = null;
// 将区域滚动到视图中心 // 将区域滚动到视图中心并播放
this.$nextTick(() => { this.$nextTick(() => {
const center = (range.start + range.end) / 2; // 开始播放
const duration = this.wavesurfer.getDuration(); this.startRegionPlayback(range.start, range.end, pageRegionId);
// 定位到中心位置
this.wavesurfer.seekTo(center / duration);
// 更新所有区域的样式 // 更新所有区域的样式
Object.keys(this.audioRegions).forEach(regionId => { Object.keys(this.audioRegions).forEach(regionId => {
// 跳过页面区间 const region = this.audioRegions[regionId];
if (regionId.startsWith('page-')) return; if (!region) return;
if (regionId.startsWith('page-')) {
region.update({
color: 'rgba(255, 192, 203, 0.3)'
});
return;
}
const [rPageIndex, groupIndex] = regionId.split('-').map(Number); const [rPageIndex, groupIndex] = regionId.split('-').map(Number);
const region = this.audioRegions[regionId];
const isCurrentPage = rPageIndex === range.pageIndex; const isCurrentPage = rPageIndex === range.pageIndex;
if (region) {
region.update({ region.update({
color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)', color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)',
drag: isCurrentPage, drag: isCurrentPage,
resize: isCurrentPage resize: isCurrentPage
}); });
}
});
console.log('Region styles updated');
}); });
}); });
};
// 验证区域元素是否存在 // 确保事件监听器只添加一次
console.log('Page region created:', { region.element.removeEventListener('click', handleClick);
pageIndex: range.pageIndex, region.element.addEventListener('click', handleClick);
pageRegionId,
hasElement: !!region.element,
element: region.element
});
} else {
console.error(`Failed to create page region ${pageRegionId}`);
} }
} catch (err) { } catch (err) {
console.error(`Error creating page region ${pageRegionId}:`, err); console.error(`Error creating page region ${pageRegionId}:`, err);
...@@ -380,16 +372,21 @@ new Vue({ ...@@ -380,16 +372,21 @@ new Vue({
this.pages.forEach((page, pageIndex) => { this.pages.forEach((page, pageIndex) => {
if (page.groups) { if (page.groups) {
page.groups.forEach((group, groupIndex) => { page.groups.forEach((group, groupIndex) => {
if (typeof group.start === 'number' && !isNaN(group.start) && // 确保时间值是有效的数字且精确到小数点后3位
typeof group.end === 'number' && !isNaN(group.end)) { const start = typeof group.start === 'number' && !isNaN(group.start) ?
Number(group.start.toFixed(3)) : null;
const end = typeof group.end === 'number' && !isNaN(group.end) ?
Number(group.end.toFixed(3)) : null;
if (start !== null && end !== null && start < end) {
const groupRegionId = `${pageIndex}-${groupIndex}`; const groupRegionId = `${pageIndex}-${groupIndex}`;
const isCurrentPage = pageIndex === this.currentPageIndex - 1; const isCurrentPage = pageIndex === this.currentPageIndex - 1;
try { try {
const region = this.wavesurfer.addRegion({ const region = this.wavesurfer.addRegion({
id: groupRegionId, id: groupRegionId,
start: group.start, start: start,
end: group.end, end: end,
color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)', color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)',
drag: isCurrentPage, drag: isCurrentPage,
resize: isCurrentPage resize: isCurrentPage
...@@ -397,8 +394,20 @@ new Vue({ ...@@ -397,8 +394,20 @@ new Vue({
if (region) { if (region) {
this.audioRegions[groupRegionId] = region; this.audioRegions[groupRegionId] = region;
} else {
console.error(`Failed to create group region ${groupRegionId}`); // 为组区间添加点击播放功能
const handleGroupClick = (event) => {
// 阻止事件冒泡
event.stopPropagation();
event.preventDefault();
// 开始播放
this.startRegionPlayback(start, end, groupRegionId);
};
// 确保事件监听器只添加一次
region.element.removeEventListener('click', handleGroupClick);
region.element.addEventListener('click', handleGroupClick);
} }
} catch (err) { } catch (err) {
console.error(`Error creating group region ${groupRegionId}:`, err); console.error(`Error creating group region ${groupRegionId}:`, err);
...@@ -511,6 +520,11 @@ new Vue({ ...@@ -511,6 +520,11 @@ new Vue({
if (!this.wavesurfer) return; if (!this.wavesurfer) return;
try { try {
// 如果正在区间播放,先清理
if (this.currentPlayingRegion) {
this.stopCurrentPlayback();
}
this.wavesurfer.playPause(); this.wavesurfer.playPause();
this.isPlaying = !this.isPlaying; this.isPlaying = !this.isPlaying;
...@@ -528,8 +542,8 @@ new Vue({ ...@@ -528,8 +542,8 @@ new Vue({
if (!this.wavesurfer) return; if (!this.wavesurfer) return;
try { try {
this.stopCurrentPlayback();
this.wavesurfer.stop(); this.wavesurfer.stop();
this.isPlaying = false;
} catch (err) { } catch (err) {
console.error('Error stopping audio:', err); console.error('Error stopping audio:', err);
this.$message.error('停止失败'); this.$message.error('停止失败');
...@@ -1142,6 +1156,46 @@ new Vue({ ...@@ -1142,6 +1156,46 @@ new Vue({
this.currentGroup = null; this.currentGroup = null;
this.selectedItemIndex = null; this.selectedItemIndex = null;
}, },
// 新增:清理播放定时器
clearPlayTimer() {
if (this.playTimer) {
clearTimeout(this.playTimer);
this.playTimer = null;
}
},
// 新增:停止当前播放
stopCurrentPlayback() {
this.clearPlayTimer();
if (this.isPlaying) {
this.wavesurfer.pause();
this.isPlaying = false;
}
this.currentPlayingRegion = null;
},
// 新增:开始区间播放
startRegionPlayback(start, end, regionId) {
// 如果正在播放,先停止
this.stopCurrentPlayback();
// 开始新的播放
this.wavesurfer.seekTo(start / this.wavesurfer.getDuration());
this.wavesurfer.play();
this.isPlaying = true;
this.currentPlayingRegion = regionId;
// 设置新的定时器
const playDuration = (end - start) * 1000; // 转换为毫秒
this.playTimer = setTimeout(() => {
if (this.isPlaying && this.currentPlayingRegion === regionId) {
this.wavesurfer.pause();
this.isPlaying = false;
this.currentPlayingRegion = null;
}
}, playDuration);
},
}, },
beforeDestroy() { beforeDestroy() {
...@@ -1149,6 +1203,9 @@ new Vue({ ...@@ -1149,6 +1203,9 @@ new Vue({
document.removeEventListener('mousemove', this.handleMouseMove); document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp); document.removeEventListener('mouseup', this.handleMouseUp);
// 清理定时器
this.clearPlayTimer();
// 销毁波形图 // 销毁波形图
if (this.wavesurfer) { if (this.wavesurfer) {
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment