readContent.vue 7.2 KB

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