Browse Source

录音增加限时

caner 1 year ago
parent
commit
3b70b4fe9b
2 changed files with 4 additions and 2 deletions
  1. 1 1
      README.md
  2. 3 1
      src/components/mic.vue

+ 1 - 1
README.md

@@ -17,5 +17,5 @@
 ### can
 - [x] 车辆信号
 - [x] 车辆电量
-- [x] 开启录音
+- [x] 限时对讲
 - [x] 开启静音

+ 3 - 1
src/components/mic.vue

@@ -15,6 +15,7 @@ import {
 } from 'vue'
 import useStore from '@/store'
 
+const props = withDefaults(defineProps<{ time?: number }>(), { time: 5000 })
 const emit = defineEmits<{(evt: 'callBack', value: Blob): void }>()
 const store = useStore()
 const notice = useNotification()
@@ -22,6 +23,7 @@ const show = ref(false)
 const rtcConnected = computed(() => store.rtcConnected)
 let chunks = [] as Blob[]
 let audio = null as null | MediaRecorder
+let timer = null as Any
 
 async function initMic() {
   try {
@@ -54,7 +56,7 @@ function change() {
   if (!rtcConnected.value) return
   show.value = !show.value
   if (!audio) return
-  if (show.value) { audio.start() } else { audio.stop() }
+  if (show.value) { audio?.start(); timer = setTimeout(() => { show.value = false; audio?.stop() }, props.time) } else { audio?.stop(); clearTimeout(timer) }
   console.log('mic', show.value)
 }