From b7b79d84b269641a4a8134249f6b5ec33ae6218f Mon Sep 17 00:00:00 2001
From: zhangchen <1652267879@qq.com>
Date: 星期六, 26 七月 2025 14:15:03 +0800
Subject: [PATCH] Merge branch 'ID1825-床旁副屏改版' into test

---
 src/composables/useAudioPlayer.ts |   66 +++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/src/composables/useAudioPlayer.ts b/src/composables/useAudioPlayer.ts
new file mode 100644
index 0000000..341dfbc
--- /dev/null
+++ b/src/composables/useAudioPlayer.ts
@@ -0,0 +1,66 @@
+import { ref, watch } from "vue";
+
+let currentAudio: HTMLAudioElement | null = null;
+
+export function useAudioPlayer() {
+  const source = ref<string | null>(null);
+  const isPlaying = ref(false);
+
+  // 播放音频
+  const play = (src: string) => {
+    if (!src) return;
+
+    // 如果当前正在播放其他音频,先暂停并释放
+    if (currentAudio) {
+      currentAudio.pause();
+      currentAudio = null;
+      isPlaying.value = false;
+    }
+
+    currentAudio = new Audio(src);
+    currentAudio.loop = true; //  循环播放
+    currentAudio.volume = 1.0; //  音量最大
+    currentAudio
+      .play()
+      .then(() => {
+        isPlaying.value = true;
+      })
+      .catch((err) => {
+        console.error("音频播放失败:", err);
+      });
+
+    // 监听播放完毕
+    currentAudio.onended = () => {
+      isPlaying.value = false;
+    };
+
+    source.value = src;
+  };
+
+  // 暂停播放
+  const pause = () => {
+    if (currentAudio) {
+      currentAudio.pause();
+      isPlaying.value = false;
+    }
+  };
+
+  // 停止播放(并清除资源)
+  const stop = () => {
+    if (currentAudio) {
+      currentAudio.pause();
+      currentAudio.currentTime = 0;
+      currentAudio = null;
+      isPlaying.value = false;
+      source.value = null;
+    }
+  };
+
+  return {
+    source,
+    isPlaying,
+    play,
+    pause,
+    stop,
+  };
+}

--
Gitblit v1.8.0