import { defineStore } from 'pinia' export const useAudioStore = defineStore('audio', { state: () => ({ soundInstance: null, // Howl 实例 isPlaying: false, // 播放状态 isVoiceEnabled: true, // 新增声音开关状态 playbackPosition: 0, // 新增播放位置存储 lastVoiceState: null, ttsUrl:'', isNewInstance: false, // 新增是否是新实例的标志 nowSound:'', currentAudioUrl: '', // 当前音频URL isPaused: false, // 是否处于暂停状态 duration: 0 // 音频总时长 }), actions: { // 设置音频实例 setAudioInstance(instance) { this.soundInstance = instance }, // 播放控制 play() { if (this.soundInstance) { if (this.isPaused && this.playbackPosition > 0) { // 从暂停位置继续播放 this.soundInstance.seek(this.playbackPosition) } this.soundInstance.play() this.isPlaying = true this.isPaused = false } }, // 暂停控制 pause() { if (this.soundInstance && this.isPlaying) { // 保存当前播放位置 this.playbackPosition = this.soundInstance.seek() || 0 this.soundInstance.pause() this.isPlaying = false this.isPaused = true } }, // 停止播放 stop() { if (this.soundInstance) { this.soundInstance.stop() this.isPlaying = false this.isPaused = false this.playbackPosition = 0 } }, // 切换播放/暂停 togglePlayPause() { if (this.isPlaying) { this.pause() } else { this.play() } }, // 设置当前音频URL setCurrentAudioUrl(url) { if (this.currentAudioUrl !== url) { // 如果是新的音频,重置播放状态 this.stop() this.currentAudioUrl = url this.playbackPosition = 0 this.isPaused = false } }, // 语音开关控制 toggleVoice() { this.isVoiceEnabled = !this.isVoiceEnabled if (!this.isVoiceEnabled) { // 关闭语音时停止当前播放 this.stop() } }, // 重置音频状态 resetAudioState() { this.stop() this.currentAudioUrl = '' this.ttsUrl = '' this.soundInstance = null this.nowSound = '' } } })