message-audio.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <div
  3. :class="{
  4. 'message-audio': true,
  5. 'reserve': props.messageItem.flow === 'out',
  6. }"
  7. @click="toggleClick"
  8. >
  9. <div class="audio-icon-container">
  10. <div :class="{ 'mask': true, 'play': isAudioPlaying }" />
  11. <Icon
  12. class="icon"
  13. width="15px"
  14. height="20px"
  15. :file="audioIcon"
  16. />
  17. </div>
  18. <div
  19. class="time"
  20. :style="{ width: `${props.content.second * 5}px` }"
  21. >
  22. {{ props.content.second || 1 }} "
  23. </div>
  24. </div>
  25. </template>
  26. <script lang="ts" setup>
  27. import { onUnmounted, ref, watch } from '../../../../adapter-vue';
  28. import { IMessageModel } from '@tencentcloud/chat-uikit-engine';
  29. import Icon from '../../../common/Icon.vue';
  30. import { Toast } from '../../../common/Toast/index';
  31. import audioIcon from '../../../../assets/icon/msg-audio.svg';
  32. import { IAudioMessageContent, IAudioContext } from '../../../../interface';
  33. interface IProps {
  34. broadcastNewAudioSrc: string;
  35. messageItem: IMessageModel;
  36. content: IAudioMessageContent;
  37. }
  38. interface IEmits {
  39. (
  40. e: 'getGlobalAudioContext',
  41. map: Map<string, IAudioContext>,
  42. options?: { newAudioSrc: string }
  43. ): void;
  44. }
  45. const emits = defineEmits<IEmits>();
  46. const props = withDefaults(defineProps<IProps>(), {
  47. messageItem: () => ({}) as IMessageModel,
  48. content: () => ({}) as IAudioMessageContent,
  49. });
  50. const audioMap = new Map<string, IAudioContext>();
  51. const isAudioPlaying = ref<boolean>(false);
  52. onUnmounted(() => {
  53. const audioContext = getAudio();
  54. if (isAudioPlaying.value) {
  55. stopAudio();
  56. }
  57. audioContext?.destroy?.();
  58. audioMap.delete('audio');
  59. });
  60. watch(() => props.broadcastNewAudioSrc, (newSrc) => {
  61. if (newSrc !== props.content.url && isAudioPlaying.value) {
  62. stopAudio();
  63. // 这一步的 audioContext 大概率已被销毁 手动执行一下暂停
  64. // The audioContext may have been destroyed. Manually execute the pause
  65. isAudioPlaying.value = false;
  66. }
  67. });
  68. function toggleClick() {
  69. emits('getGlobalAudioContext', audioMap, { newAudioSrc: props.content.url });
  70. if (props.messageItem.hasRiskContent || !props.content.url) {
  71. Toast({
  72. message: '暂不支持播放',
  73. });
  74. return;
  75. }
  76. // audioContext 会被缓存 必须拉取一次
  77. // audioContext will be cached, it must be get first
  78. const audioContext = getAudio();
  79. if (!audioContext) {
  80. audioMap.set('audio', uni.createInnerAudioContext() as IAudioContext);
  81. // #ifdef MP
  82. uni.setInnerAudioOption({
  83. obeyMuteSwitch: false,
  84. });
  85. // #endif
  86. initAudioSrc();
  87. }
  88. toggleAudioPlayState();
  89. }
  90. function toggleAudioPlayState() {
  91. if (!isAudioPlaying.value) {
  92. playAudio();
  93. } else {
  94. stopAudio();
  95. }
  96. }
  97. function initAudioSrc() {
  98. const audioContext = getAudio();
  99. if (!audioContext) {
  100. return;
  101. }
  102. audioContext.src = props.content.url;
  103. isAudioPlaying.value = false;
  104. audioContext.onPlay(onAudioPlay);
  105. audioContext.onStop(onAudioStop);
  106. audioContext.onEnded(onAudioEnded);
  107. audioContext.onError(onAudioError);
  108. }
  109. function playAudio() {
  110. const audioContext = getAudio();
  111. if (!audioContext) {
  112. return;
  113. }
  114. audioContext.play();
  115. }
  116. function stopAudio() {
  117. const audioContext = getAudio();
  118. if (!audioContext) {
  119. return;
  120. }
  121. try {
  122. // audiocontext 的内存对象还在 但可能播放实例已被销毁
  123. // The memory of the audiocontext is in memory. But The play instance may have been destroyed.
  124. audioContext.stop();
  125. } catch {
  126. // ignore
  127. }
  128. }
  129. function onAudioPlay() {
  130. isAudioPlaying.value = true;
  131. }
  132. function onAudioStop() {
  133. isAudioPlaying.value = false;
  134. }
  135. function onAudioEnded() {
  136. isAudioPlaying.value = false;
  137. }
  138. function onAudioError() {
  139. console.warn('audio played error');
  140. }
  141. function getAudio(): IAudioContext | undefined {
  142. return audioMap.get('audio');
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. $flow-in-bg-color: #fbfbfb;
  147. $flow-out-bg-color: #dceafd;
  148. :not(not) {
  149. display: flex;
  150. flex-direction: column;
  151. box-sizing: border-box;
  152. min-width: 0;
  153. }
  154. .message-audio {
  155. flex-direction: row;
  156. flex: 0 0 auto;
  157. cursor: pointer;
  158. -webkit-tap-highlight-color: transparent;
  159. overflow: hidden;
  160. .audio-icon-container {
  161. // uniapp中会有一些偏差 这里 width 要比图标略宽
  162. width: 16px;
  163. height: 20px;
  164. position: relative;
  165. flex: 0 0 auto;
  166. flex-direction: row;
  167. justify-content: flex-end;
  168. margin: 0 7px 0 0;
  169. overflow: hidden;
  170. .mask {
  171. position: absolute;
  172. z-index: 1;
  173. width: 105%;
  174. height: 105%;
  175. left: 0;
  176. top: 0;
  177. transform-origin: right;
  178. transform: scaleX(0);
  179. background-color: $flow-in-bg-color;
  180. &.play {
  181. animation: audio-play 2s steps(1, end) infinite;
  182. }
  183. }
  184. }
  185. @keyframes audio-play {
  186. 0% {
  187. transform: scaleX(0.7056);
  188. }
  189. 50% {
  190. transform: scaleX(0.3953);
  191. }
  192. 75% {
  193. transform: scaleX(0);
  194. visibility: hidden;
  195. }
  196. 100% {
  197. transform: scaleX(0);
  198. visibility: hidden;
  199. }
  200. }
  201. .time {
  202. max-width: 165px;
  203. min-width: 20px;
  204. text-align: start;
  205. white-space: nowrap;
  206. }
  207. &.reserve {
  208. flex-direction: row-reverse;
  209. .time {
  210. text-align: end;
  211. }
  212. .audio-icon-container {
  213. margin: 0 0 0 7px;
  214. .mask {
  215. transform-origin: left;
  216. background-color: $flow-out-bg-color;
  217. }
  218. }
  219. .icon {
  220. transform: rotate(180deg);
  221. }
  222. }
  223. }
  224. </style>