Commit 0d5b7a31 authored by liujiangnan's avatar liujiangnan

feat: 联动播放

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