liming 1 week ago
parent
commit
6d7f6ded6a
1 changed files with 28 additions and 1 deletions
  1. 28 1
      src/views/im/chat/index.vue

+ 28 - 1
src/views/im/chat/index.vue

@@ -52,7 +52,7 @@
     <!-- 输入框 -->
     <div class="page-foot">
       <div class="flex-box">
-        <svg-icon class="page-icon" name="voice" />
+        <svg-icon class="page-icon" name="voice" @mousedown="startAudio" />
         <van-field
           rows="1"
           type="textarea"
@@ -102,6 +102,7 @@ import { Keyboard } from "@capacitor/keyboard";
 import { useWebSocketStore } from "@/stores/modules/webSocketStore.js";
 import { useWalletStore } from "@/stores/modules/walletStore.js";
 import { MSG_TYPE, MESSAGE_TYPE_USER } from "@/common/constant/msgType";
+import { Capacitor } from '@capacitor/core';
 
 const router = useRouter();
 const route = useRoute()
@@ -116,6 +117,9 @@ const showEmoji = ref(false);
 const showTools = ref(false);
 const appBoxHeight = ref(260); // 面板高度,可根据需要调整
 
+// 语音
+const mediaRecorder = ref(null); 
+
 // 计算当前底部总高度
 const currentBottomHeight = computed(() => {
   // 优先级:键盘 > 表情面板 > 工具面板 > 默认高度
@@ -172,6 +176,29 @@ const goDetail = () => {
   router.push("detail");
 };
 
+const startAudio = async () => {
+    try {
+    // 请求麦克风权限
+    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+    
+    // 创建 MediaRecorder 实例
+    mediaRecorder.value = new MediaRecorder(stream);
+    
+    // 收集音频数据
+    mediaRecorder.value.ondataavailable = (e) => {
+      audioChunks.push(e.data);
+    };
+    
+    mediaRecorder.value.onerror = (e) => {
+      console.error('Recording error:', e.error);
+    };
+    
+    mediaRecorder.value.start();
+    console.log('Recording started');
+  } catch (error) {
+    console.error('Error accessing microphone:', error);
+  }
+}
 // 发送消息
 const sendMessage = () => {
   if (!text.value.trim()) {