readContent.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <!-- 单词区 && 音标区:最多16位,超过换行 -->
  2. <!-- 单音节最长:swimming 多音节最长:transportation -->
  3. <template>
  4. <selectTypesVue activeSelect="3"></selectTypesVue>
  5. <view class="ezy-border-body">
  6. <view class="words-du-box">
  7. <view class="du-body-box">
  8. <!-- 单词区 -->
  9. <view class="word-circle-box">{{data.name}}</view>
  10. <!-- 音标区 -->
  11. <view class="yb-play-box du-yb-play-box">
  12. <yinbiaoTxtVue :yinbiao="activeWord.yinbiao"></yinbiaoTxtVue>
  13. <!-- 音频播放 -->
  14. <audioTwoVue :active-word="activeWord" @play-audio="handlePlay"></audioTwoVue>
  15. </view>
  16. <view class="pin-words-explain-box du-words-explain-box">
  17. <view class="words-explain-item" v-if="data.jianyi&&data.jianyi.length>0"
  18. v-for="(item,index) in data.jianyi" :key="index">
  19. {{item}}
  20. </view>
  21. </view>
  22. </view>
  23. <view class="mike-play-box">
  24. <view class="mike-play-tip" v-if="isRecording">录音中...</view>
  25. <view class="mike-play-tip" v-else>长按 读一读</view>
  26. <!-- <view class="status">{{ recordingStatus }}</view> -->
  27. <!-- <view class="duration" v-if="isRecording">录音时长: {{ Math.floor(duration) }}秒</view> -->
  28. <view class="du-btn-box">
  29. <button class="play-btn" :class="{ 'playing-btn': isPlaying}" @click="playVoice"
  30. v-if="voicePath"></button>
  31. <button class="mike-btn" :class="{ 'mike-az-btn': isRecording}" @touchstart="handleTouchStart"
  32. @touchend="handleTouchEnd" @touchcancel="handleTouchEnd" :disabled="isPlaying">
  33. <!--{{ isRecording ? '松开结束' : '按住录音' }}
  34. <view v-if="isPlaying" class="disabled-mask">播放中不可录音</view> -->
  35. </button>
  36. </view>
  37. <!-- <button class="play-btn" :class="{ disabled: isRecording || !voicePath }" @click="playVoice"
  38. :disabled="isRecording || !voicePath">
  39. {{ isPlaying ? '播放中...' : '播放录音' }}
  40. <view v-if="isRecording" class="disabled-mask">录音中不可播放</view>
  41. </button> -->
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup>
  47. import selectTypesVue from './selectTypes.vue';
  48. import audioTwoVue from './audioTwo.vue';
  49. import yinbiaoTxtVue from "./yinbiaoTxt.vue"
  50. import {
  51. onLoad
  52. } from "@dcloudio/uni-app"
  53. import {
  54. reactive,
  55. ref,
  56. onMounted,
  57. onUnmounted
  58. } from 'vue';
  59. const props = defineProps({
  60. activeWord: {
  61. type: Object,
  62. },
  63. pageData: {
  64. type: Object,
  65. },
  66. activeWords: {
  67. type: Array
  68. },
  69. })
  70. const emits = defineEmits(['play-audio'])
  71. let tabFlag = ref(1)
  72. const audioInfo = ref(null)
  73. const data = reactive({
  74. name: '',
  75. yinbiao: '',
  76. jianyi: []
  77. })
  78. // 录音相关状态
  79. const isRecording = ref(false)
  80. const isPlaying = ref(false)
  81. const voicePath = ref('')
  82. const duration = ref(0)
  83. const timer = ref(null)
  84. const recordingStatus = ref('准备就绪')
  85. const startTime = ref(0)
  86. const longPressTimer = ref(null) // 长按计时器
  87. const isRecorderReady = ref(true) // 新增:录音器就绪状态
  88. // 获取录音和音频播放管理器
  89. const recorderManager = uni.getRecorderManager()
  90. const innerAudioContext = uni.createInnerAudioContext()
  91. // 初始化录音器
  92. const initRecorder = () => {
  93. recorderManager.onStart(() => {
  94. console.log('recorder start')
  95. isRecording.value = true
  96. isRecorderReady.value = true // 录音结束后标记为就绪
  97. // recordingStatus.value = '录音中...'
  98. // startTime.value = Date.now()
  99. // startTimer()
  100. })
  101. recorderManager.onStop((res) => {
  102. // const recordTime = (Date.now() - startTime.value) / 1000 // 计算实际录音时间
  103. isRecording.value = false
  104. isRecorderReady.value = true // 录音结束后标记为就绪
  105. // if (recordTime < 3) {
  106. // uni.showToast({
  107. // title: '录音时间太短,需3秒以上',
  108. // icon: 'none'
  109. // })
  110. // return
  111. // }
  112. if (res.tempFilePath) {
  113. voicePath.value = res.tempFilePath
  114. uni.showToast({
  115. title: '录音成功',
  116. icon: 'success'
  117. })
  118. } else {
  119. uni.showToast({
  120. title: '录音失败',
  121. icon: 'none'
  122. })
  123. }
  124. })
  125. recorderManager.onError((res) => {
  126. console.error('recorder error', res)
  127. //stopTimer()
  128. isRecording.value = false
  129. isRecorderReady.value = true // 出错时也标记为就绪
  130. setTimeout(() => {
  131. initRecorder()
  132. }, 300)
  133. //recordingStatus.value = `录音错误: ${res.errMsg}`
  134. // uni.showToast({
  135. // title: `录音出错: ${res.errMsg}`,
  136. // icon: 'none'
  137. // })
  138. })
  139. innerAudioContext.onPlay(() => {
  140. isPlaying.value = true
  141. //recordingStatus.value = '播放中...'
  142. })
  143. innerAudioContext.onEnded(() => {
  144. isPlaying.value = false
  145. //recordingStatus.value = '播放完成'
  146. })
  147. innerAudioContext.onError((res) => {
  148. isPlaying.value = false
  149. console.error('play error', res)
  150. uni.showToast({
  151. title: '播放失败',
  152. icon: 'none'
  153. })
  154. })
  155. }
  156. async function handlePlay(opt) {
  157. emits('play-audio', opt)
  158. }
  159. // 处理触摸开始(移动端)
  160. const handleTouchStart = (e) => {
  161. e.preventDefault()
  162. if (isPlaying.value) return
  163. // 设置长按计时器,500ms后才开始录音
  164. longPressTimer.value = setTimeout(() => {
  165. startRecording()
  166. }, 400)
  167. }
  168. // 处理触摸结束(移动端)
  169. const handleTouchEnd = (e) => {
  170. e.preventDefault()
  171. clearTimeout(longPressTimer.value)
  172. if (isRecording.value) {
  173. endRecording()
  174. }
  175. }
  176. // 处理触摸取消(移动端,如被系统中断)
  177. const handleTouchCancel = (e) => {
  178. handleTouchEnd(e) // 与touchend同样处理
  179. }
  180. // 开始录音
  181. const startRecording = () => {
  182. if (isRecording.value || !isRecorderReady.value) return
  183. console.log('开始录音')
  184. //recordingStatus.value = '准备录音...'
  185. isRecording.value = true // 提前设置状态,避免延迟
  186. isRecorderReady.value = false
  187. // startTime.value = Date.now()
  188. // startTimer()
  189. const options = {
  190. duration: 60000,
  191. sampleRate: 44100,
  192. numberOfChannels: 1,
  193. encodeBitRate: 192000,
  194. format: 'mp3',
  195. frameSize: 50
  196. }
  197. try {
  198. recorderManager.start(options)
  199. } catch (e) {
  200. console.error('启动录音失败:', e)
  201. isRecording.value = false
  202. isRecorderReady.value = true
  203. uni.showToast({
  204. title: '启动录音失败,请重试',
  205. icon: 'none'
  206. })
  207. }
  208. }
  209. // 结束录音
  210. const endRecording = () => {
  211. if (!isRecording.value) return
  212. console.log('停止录音')
  213. try {
  214. recorderManager.stop()
  215. } catch (e) {
  216. console.error('停止录音失败:', e)
  217. isRecording.value = false
  218. isRecorderReady.value = true
  219. }
  220. }
  221. const getRecordingDuration = () => {
  222. if (!isRecording.value) return 0
  223. return Math.floor((Date.now() - startTime.value) / 1000)
  224. }
  225. // 播放录音
  226. const playVoice = () => {
  227. if (isRecording.value || !voicePath.value) return
  228. console.log('播放录音:', voicePath.value)
  229. innerAudioContext.src = voicePath.value
  230. innerAudioContext.play()
  231. }
  232. // // 计时器相关
  233. // const startTimer = () => {
  234. // duration.value = 0
  235. // const start = Date.now()
  236. // timer.value = setInterval(() => {
  237. // // 计算实际经过的时间(毫秒),然后转换为秒
  238. // const elapsed = Math.floor((Date.now() - start) / 1000)
  239. // duration.value = elapsed
  240. // }, 200) // 缩短检查间隔,但计算基于实际时间差
  241. // }
  242. // const stopTimer = () => {
  243. // if (timer.value) {
  244. // clearInterval(timer.value)
  245. // timer.value = null
  246. // }
  247. // duration.value = 0
  248. // }
  249. // 初始化单词数据
  250. const initItem = () => {
  251. data.name = props.activeWord.name;
  252. data.yinbiao = props.activeWord.yinbiao;
  253. data.jianyi = props.activeWord.jianyi;
  254. console.log('data', data);
  255. }
  256. onMounted(() => {
  257. initItem()
  258. initRecorder()
  259. })
  260. onUnmounted(() => {
  261. //stopTimer()
  262. clearTimeout(longPressTimer.value)
  263. try {
  264. recorderManager.stop()
  265. innerAudioContext.stop()
  266. innerAudioContext.destroy()
  267. } catch (e) {
  268. console.error('清理资源时出错:', e)
  269. }
  270. })
  271. </script>