readContent.vue 8.2 KB

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