// 音频播放蒙层组件 Component({ properties: { // 是否显示蒙层 isPlaying: { type: Boolean, value: false }, // 播放的文本内容 audioText: { type: String, value: '' } }, data: { progress: 0, // 播放进度百分比 currentTime: '00:00', // 当前播放时间 duration: '00:00', // 总时长 timer: null // 定时器 }, lifetimes: { detached() { // 组件销毁时清理定时器 this.clearTimer(); } }, methods: { // 初始化音频监听 initAudio(audioContext) { if (!audioContext) return; this.audioContext = audioContext; // 监听音频播放进度 audioContext.onTimeUpdate(() => { const currentTime = audioContext.currentTime || 0; const duration = audioContext.duration || 0; this.setData({ progress: duration > 0 ? (currentTime / duration) * 100 : 0, currentTime: this.formatTime(currentTime), duration: this.formatTime(duration) }); }); // 监听播放结束 audioContext.onEnded(() => { this.stopPlaying(); }); // 监听播放错误 audioContext.onError(() => { this.stopPlaying(); }); // 监听播放停止 audioContext.onStop(() => { this.stopPlaying(); }); }, // 格式化时间显示 formatTime(seconds) { if (!seconds || isNaN(seconds)) return '00:00'; const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`; }, // 停止播放 stopPlaying() { this.setData({ progress: 0, currentTime: '00:00', duration: '00:00' }); this.clearTimer(); // 触发父组件的停止事件 this.triggerEvent('stop'); }, // 清理定时器 clearTimer() { if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null }); } }, // 点击蒙层背景 onOverlayTap() { // 点击背景关闭蒙层并停止播放 this.stopPlaying(); }, // 点击停止按钮 onStopTap() { this.stopPlaying(); }, // 阻止事件冒泡 stopPropagation() { // 阻止点击内容区域时触发背景点击事件 } } });