Commit 065fc7f4 authored by liujiangnan's avatar liujiangnan

feat

parent 129fedbd
......@@ -67,6 +67,8 @@ new Vue({
// 添加全局事件监听
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
// 添加空格键控制播放/暂停的事件监听
document.addEventListener('keydown', this.handleKeydown);
});
},
......@@ -195,16 +197,17 @@ new Vue({
progressColor: '#3B8686',
cursorColor: '#0B486B',
height: 128,
minPxPerSec: 0,
fillParent: true, // 初始状态填充父容器
scrollParent: false, // 初始状态不显示滚动条
responsive: true, // 响应容器大小变化
normalize: true, // 波形归一化
pixelRatio: 1, // 像素比
autoCenter: true, // 自动居中
cursorWidth: 2, // 光标宽度
interact: true, // 允许交互
backend: 'MediaElement', // 使用 MediaElement 后端代替默认的 WebAudio
minPxPerSec: 10, // 调整最小像素/秒,使初始显示更合理
maxPxPerSec: 200, // 添加最大像素/秒限制
fillParent: true, // 初始状态填充父容器
scrollParent: false, // 初始状态不显示滚动条
normalize: true, // 波形归一化
pixelRatio: window.devicePixelRatio || 1, // 使用设备像素比
autoCenter: true, // 自动居中
cursorWidth: 2, // 光标宽度
interact: true, // 允许交互
responsive: true, // 响应容器大小变化
backend: 'MediaElement',
plugins: [
WaveSurfer.regions.create({
dragSelection: false // 禁用拖动选择,避免与我们的区域交互冲突
......@@ -213,11 +216,13 @@ new Vue({
container: '#wave-timeline',
height: 20,
notchPercentHeight: 50,
primaryFontColor: 'rgba(102, 102, 102, 0.7)',
secondaryFontColor: 'rgba(136, 136, 136, 0.5)',
primaryColor: 'rgba(102, 102, 102, 0.7)',
secondaryColor: 'rgba(136, 136, 136, 0.5)',
style: 'background-color: rgba(255, 255, 255, 0.1);'
primaryFontColor: '#666',
secondaryFontColor: '#888',
primaryColor: '#666',
secondaryColor: '#888',
timeInterval: 0.1,
primaryLabelInterval: 1,
secondaryLabelInterval: 0.5
})
]
});
......@@ -225,6 +230,7 @@ new Vue({
// 监听事件
this.wavesurfer.on('ready', () => {
this.audioLoading = false;
console.log('WaveSurfer is ready'); // 添加日志
// 添加一个小延时确保 WaveSurfer 完全准备好
setTimeout(() => {
......@@ -236,6 +242,18 @@ new Vue({
}, 100);
});
// 添加错误处理
this.wavesurfer.on('error', (err) => {
console.error('WaveSurfer error:', err);
this.$message.error('音频加载失败:' + err.message);
this.audioLoading = false;
});
// 添加加载进度处理
this.wavesurfer.on('loading', (progress) => {
console.log('Loading progress:', progress); // 添加日志
});
// 添加点击波形定位事件
this.wavesurfer.on('click', (e, progress) => {
if (this.isPageLocked) {
......@@ -285,12 +303,6 @@ new Vue({
}
});
this.wavesurfer.on('error', (err) => {
console.error('WaveSurfer error:', err);
this.$message.error('音频加载失败');
this.audioLoading = false;
});
// 加载音频
this.audioLoading = true;
this.wavesurfer.load(this.audioPoints.audio_url);
......@@ -335,7 +347,7 @@ new Vue({
if (region) {
this.audioRegions[pageRegionId] = region;
// 添加点击事件处理
// 修改点击事件处理
const handleClick = (event) => {
event.stopPropagation();
event.preventDefault();
......@@ -350,10 +362,12 @@ new Vue({
this.selectedGroup = null;
this.currentGroup = null;
this.$nextTick(() => {
this.startRegionPlayback(page.start, page.end, pageRegionId);
this.updateRegionsStyle();
});
// 更新区域样式
this.updateRegionsStyle();
// 定位到区间开始位置
const duration = this.wavesurfer.getDuration();
this.wavesurfer.seekTo(page.start / duration);
};
region.element.removeEventListener('click', handleClick);
......@@ -412,11 +426,14 @@ new Vue({
if (region) {
this.audioRegions[groupRegionId] = region;
// 添加点击事件
// 修改点击事件处理
const handleClick = (event) => {
event.stopPropagation();
event.preventDefault();
this.startRegionPlayback(group.start, group.end, groupRegionId);
// 定位到区间开始位置
const duration = this.wavesurfer.getDuration();
this.wavesurfer.seekTo(group.start / duration);
};
region.element.removeEventListener('click', handleClick);
region.element.addEventListener('click', handleClick);
......@@ -501,13 +518,13 @@ new Vue({
this.zoomLevel = zoomLevel;
this.wavesurfer.zoom(zoomLevel);
// 将区域滚动到视图中心
// 将区域滚动到视图中心并定位到开始位置
this.$nextTick(() => {
const center = (pageStart + pageEnd) / 2;
const duration = this.wavesurfer.getDuration();
// 先定位到中心位置
this.wavesurfer.seekTo(center / duration);
// 定位到开始位置
this.wavesurfer.seekTo(pageStart / duration);
// 确保波形图滚动到正确位置
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
......@@ -559,6 +576,13 @@ new Vue({
this.wavesurfer.playPause();
this.isPlaying = !this.isPlaying;
// 添加:如果停止播放,强制重绘
if (!this.isPlaying) {
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
}
// 如果是锁定状态下的播放,需要在结束位置停止
if (this.isPageLocked && this.isPlaying) {
const remainingTime = (this.lockedPageEnd - this.wavesurfer.getCurrentTime()) * 1000;
......@@ -566,6 +590,11 @@ new Vue({
if (this.isPlaying) {
this.wavesurfer.pause();
this.isPlaying = false;
// 添加:播放结束后强制重绘
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
}
}, remainingTime);
}
......@@ -617,46 +646,76 @@ new Vue({
if (!this.wavesurfer) return;
try {
// 保存当前位置
// 保存当前位置和状态
const currentTime = this.wavesurfer.getCurrentTime();
const duration = this.wavesurfer.getDuration();
const progress = currentTime / duration;
// 更新缩放设置
if (value === this.minZoom) {
// 最小缩放级别时,填充父容器
this.wavesurfer.params.fillParent = true;
this.wavesurfer.params.scrollParent = false;
} else {
// 放大时,启用滚动
this.wavesurfer.params.fillParent = false;
this.wavesurfer.params.scrollParent = true;
}
// 更新波形图参数
this.wavesurfer.params.fillParent = value === this.minZoom;
this.wavesurfer.params.scrollParent = value !== this.minZoom;
// 应用缩放
// 获取当前视图状态
const oldWidth = this.wavesurfer.drawer.width;
const oldScroll = this.wavesurfer.drawer.wrapper.scrollLeft;
const containerWidth = this.wavesurfer.drawer.wrapper.clientWidth;
// 应用新的缩放级别
this.wavesurfer.zoom(value);
// 重新定位到之前的位置
if (value > this.minZoom) {
// 使用保存的进度或当前进度作为锚点
const targetProgress = this.currentProgress || progress;
this.wavesurfer.seekTo(targetProgress);
// 使用 Promise 确保缩放操作完成
Promise.resolve().then(() => {
// 重新计算波形图
const peaks = this.wavesurfer.backend.getPeaks(this.wavesurfer.drawer.width, 0, duration);
this.wavesurfer.drawer.drawPeaks(peaks, this.wavesurfer.drawer.width, 0, duration);
// 确保波形图滚动到正确位置
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
const containerWidth = this.wavesurfer.drawer.wrapper.clientWidth;
const scrollPosition = Math.max(0, (targetProgress * scrollWidth) - (containerWidth / 2));
this.wavesurfer.drawer.wrapper.scrollLeft = scrollPosition;
} else {
// 缩小到原始大小时,重置滚动位置
this.wavesurfer.drawer.wrapper.scrollLeft = 0;
this.currentProgress = null;
}
// 重绘以确保显示正确
this.wavesurfer.drawBuffer();
// 处理滚动位置
if (value > this.minZoom) {
const targetProgress = this.currentProgress || progress;
if (this.isPageLocked) {
// 锁定状态:确保显示完整的锁定区间
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
const startPos = (this.lockedPageStart / duration) * scrollWidth;
const endPos = (this.lockedPageEnd / duration) * scrollWidth;
const regionWidth = endPos - startPos;
// 计算并应用滚动位置
requestAnimationFrame(() => {
const scrollPosition = Math.max(0, startPos - (containerWidth - regionWidth) / 2);
this.wavesurfer.drawer.wrapper.scrollLeft = scrollPosition;
// 确保重新渲染可见区域
this.wavesurfer.drawer.updateProgress(this.wavesurfer.backend.getPlayedPercents());
this.wavesurfer.fireEvent('scroll');
});
} else {
// 非锁定状态:保持当前位置居中
requestAnimationFrame(() => {
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
const scrollPosition = Math.max(0, (targetProgress * scrollWidth) - (containerWidth / 2));
this.wavesurfer.drawer.wrapper.scrollLeft = scrollPosition;
// 确保重新渲染可见区域
this.wavesurfer.drawer.updateProgress(this.wavesurfer.backend.getPlayedPercents());
this.wavesurfer.fireEvent('scroll');
});
}
} else {
// 最小缩放级别:重置所有状态
this.wavesurfer.drawer.wrapper.scrollLeft = 0;
this.currentProgress = null;
// 确保完整渲染
requestAnimationFrame(() => {
this.wavesurfer.drawer.updateProgress(this.wavesurfer.backend.getPlayedPercents());
this.wavesurfer.fireEvent('scroll');
});
}
});
} catch (err) {
console.error('Error zooming:', err);
this.$message.error('缩放失败');
}
},
......@@ -1223,6 +1282,11 @@ new Vue({
if (this.isPlaying) {
this.wavesurfer.pause();
this.isPlaying = false;
// 添加:强制重绘波形图
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
}
this.currentPlayingRegion = null;
},
......@@ -1260,6 +1324,11 @@ new Vue({
this.wavesurfer.pause();
this.isPlaying = false;
this.currentPlayingRegion = null;
// 添加:播放结束后强制重绘
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
}
}, playDuration);
},
......@@ -1296,7 +1365,7 @@ new Vue({
const startMarker = this.wavesurfer.addRegion({
id: `${pageRegionId}-start`,
start: this.lockedPageStart,
end: this.lockedPageStart + 0.000001, // 使用更小的差值
end: this.lockedPageStart + 0.000001,
color: 'rgba(0, 255, 0, 0.8)',
drag: false,
resize: false
......@@ -1305,7 +1374,7 @@ new Vue({
// 创建结束标记(红色竖线)
const endMarker = this.wavesurfer.addRegion({
id: `${pageRegionId}-end`,
start: this.lockedPageEnd - 0.000001, // 使用更小的差值
start: this.lockedPageEnd - 0.000001,
end: this.lockedPageEnd,
color: 'rgba(255, 0, 0, 0.8)',
drag: false,
......@@ -1322,12 +1391,10 @@ new Vue({
const endElement = endMarker.element;
if (startElement) {
// startElement.style.width = '1px';
startElement.style.transform = 'translateX(-0.5px)';
}
if (endElement) {
// endElement.style.width = '1px';
endElement.style.transform = 'translateX(-0.5px)';
}
});
......@@ -1364,24 +1431,65 @@ new Vue({
});
}
// 计算并应用缩放
// 设置波形图参数
this.wavesurfer.params.fillParent = false;
this.wavesurfer.params.scrollParent = true;
// 计算当前选中区间的时长和理想显示宽度
const duration = this.lockedPageEnd - this.lockedPageStart;
const padding = duration * 0.1;
const displayStart = Math.max(0, this.lockedPageStart - padding);
const displayEnd = Math.min(this.wavesurfer.getDuration(), this.lockedPageEnd + padding);
const displayDuration = displayEnd - displayStart;
const containerWidth = this.wavesurfer.drawer.wrapper.clientWidth;
const totalDuration = this.wavesurfer.getDuration();
const zoomLevel = Math.min(500, Math.floor((totalDuration / displayDuration) * 1.25));
// 先设置缩放
this.zoomLevel = zoomLevel;
this.wavesurfer.zoom(zoomLevel);
// 等待缩放完成后再调整位置
this.$nextTick(() => {
// 将视图中心定位到锁定区间中心
const center = (this.lockedPageStart + this.lockedPageEnd) / 2;
this.wavesurfer.seekTo(center / totalDuration);
// 计算当前缩放下选中区间的实际宽度
const currentPixelsPerSecond = (containerWidth / totalDuration) * (this.zoomLevel / 50);
const currentWidth = currentPixelsPerSecond * duration;
// 目标宽度是容器的80%
const targetWidth = containerWidth * 0.8;
// 先重置缩放确保渲染正确
this.wavesurfer.zoom(1);
// 等待一帧后应用实际缩放
requestAnimationFrame(() => {
// 只有当前宽度小于目标宽度时才调整缩放
if (currentWidth < targetWidth) {
// 计算需要的缩放倍数
const ratio = targetWidth / currentWidth;
const newZoomLevel = Math.floor(this.zoomLevel * ratio);
// 限制最大缩放值
this.zoomLevel = Math.min(50, Math.max(this.minZoom, newZoomLevel));
}
// 应用缩放并强制重绘
this.wavesurfer.zoom(this.zoomLevel);
this.wavesurfer.drawBuffer();
// 确保位置正确
this.$nextTick(() => {
// 计算区间中心点
const center = (this.lockedPageStart + this.lockedPageEnd) / 2;
const centerProgress = center / totalDuration;
// 定位到中心点
this.wavesurfer.seekTo(centerProgress);
// 计算滚动位置,确保选中区间居中显示
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
const containerWidth = this.wavesurfer.drawer.wrapper.clientWidth;
const startPos = (this.lockedPageStart / totalDuration) * scrollWidth;
const endPos = (this.lockedPageEnd / totalDuration) * scrollWidth;
const regionWidth = endPos - startPos;
const scrollPosition = Math.max(0, startPos - (containerWidth - regionWidth) / 2);
this.wavesurfer.drawer.wrapper.scrollLeft = scrollPosition;
// 再次强制重绘以确保显示正确
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
});
});
this.$message.success('页面已锁定');
......@@ -1407,6 +1515,15 @@ new Vue({
// 恢复到之前的缩放级别
if (this.previousZoomLevel !== null) {
// 重置波形图参数
if (this.previousZoomLevel === 1) {
this.wavesurfer.params.fillParent = true;
this.wavesurfer.params.scrollParent = false;
} else {
this.wavesurfer.params.fillParent = false;
this.wavesurfer.params.scrollParent = true;
}
this.zoomLevel = this.previousZoomLevel;
this.wavesurfer.zoom(this.previousZoomLevel);
this.previousZoomLevel = null;
......@@ -1530,12 +1647,48 @@ new Vue({
}
});
},
// 添加键盘事件处理
handleKeydown(event) {
// 如果正在输入文本,不处理空格键
if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
return;
}
// 处理空格键
if (event.code === 'Space') {
event.preventDefault(); // 阻止页面滚动
// 如果页面已锁定,检查当前位置是否在锁定范围内
if (this.isPageLocked) {
const currentTime = this.wavesurfer.getCurrentTime();
if (currentTime < this.lockedPageStart || currentTime > this.lockedPageEnd) {
this.wavesurfer.seekTo(this.lockedPageStart / this.wavesurfer.getDuration());
}
}
// 切换播放/暂停状态
this.wavesurfer.playPause();
this.isPlaying = !this.isPlaying;
// 如果开始播放,保存当前位置作为缩放锚点
if (this.isPlaying) {
this.currentProgress = this.wavesurfer.getCurrentTime() / this.wavesurfer.getDuration();
} else {
// 如果停止播放,强制重绘
requestAnimationFrame(() => {
this.wavesurfer.drawBuffer();
});
}
}
},
},
beforeDestroy() {
// 清理事件监听
document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp);
document.removeEventListener('keydown', this.handleKeydown);
// 清理定时器
this.clearPlayTimer();
......
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