@@ -162,6 +162,11 @@ export default {
162162 if (this .audio ) {
163163 this .audio .pause ();
164164
165+ // Revoke object URL if it exists
166+ if (this .audio .src .startsWith (' blob:' )) {
167+ URL .revokeObjectURL (this .audio .src );
168+ }
169+
165170 // Remove specific timeupdate handler if it exists
166171 if (this .audio ._timeUpdateHandler ) {
167172 this .audio .removeEventListener (' timeupdate' , this .audio ._timeUpdateHandler );
@@ -191,15 +196,20 @@ export default {
191196 this .audioContext = new (window .AudioContext || window .webkitAudioContext )();
192197 }
193198
194- // 直接使用 URL,不需要额外处理
199+ // 获取音频数据并创建Blob URL
195200 const response = await fetch (url);
196201 const arrayBuffer = await response .arrayBuffer ();
197202
198- // Decode the audio data
199- const audioBuffer = await this .audioContext .decodeAudioData (arrayBuffer);
203+ // 创建Blob并生成URL
204+ const blob = new Blob ([arrayBuffer], { type: this .audioType });
205+ const audioUrl = URL .createObjectURL (blob);
200206
201- this .audio = new Audio (url);
202- this .currentPlayingUrl = url;
207+ // 使用Blob URL创建Audio对象
208+ this .audio = new Audio (audioUrl);
209+ this .currentPlayingUrl = url; // 保存原始URL用于比较
210+
211+ // Decode the audio data for waveform
212+ const audioBuffer = await this .audioContext .decodeAudioData (arrayBuffer .slice (0 ));
203213
204214 // Setup event listeners
205215 this .setupAudioEventListeners ();
@@ -214,6 +224,11 @@ export default {
214224 // Start playback
215225 await this .audio .play ();
216226 this .isPlaying = true ;
227+
228+ // 添加cleanup函数
229+ this .audio .addEventListener (' ended' , () => {
230+ URL .revokeObjectURL (audioUrl);
231+ });
217232 } catch (error) {
218233 console .error (' Failed to initialize audio:' , error);
219234 this .resetPlaybackState ();
@@ -346,11 +361,17 @@ export default {
346361 setupAudioEventListeners () {
347362 // Duration metadata loaded
348363 this .audio .addEventListener (' loadedmetadata' , () => {
349- this .duration = this .audio .duration ;
364+ this .duration = this .audio .duration || 0 ;
350365 });
351366
352367 // Progress update
353- this .audio .addEventListener (' timeupdate' , this .updateProgress );
368+ this .audio .addEventListener (' timeupdate' , () => {
369+ if (this .audio ) {
370+ this .currentTime = this .audio .currentTime || 0 ;
371+ this .duration = this .audio .duration || 0 ; // 确保duration也被更新
372+ this .audioProgress = (this .audio .currentTime / this .audio .duration ) * 100 ;
373+ }
374+ });
354375
355376 // Playback ended
356377 this .audio .addEventListener (' ended' , this .handlePlaybackEnded );
@@ -393,6 +414,7 @@ export default {
393414 this .currentPlayingUrl = null ;
394415 this .audioProgress = 0 ;
395416 this .currentTime = 0 ;
417+ this .duration = 0 ; // 重置duration
396418 },
397419
398420 updateProgress () {
0 commit comments