readContent.vue 7.3 KB

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