message-audio.vue 5.2 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. (e: 'setAudioPlayed', messageID: string): void;
  45. }
  46. const emits = defineEmits<IEmits>();
  47. const props = withDefaults(defineProps<IProps>(), {
  48. messageItem: () => ({}) as IMessageModel,
  49. content: () => ({}) as IAudioMessageContent,
  50. });
  51. const audioMap = new Map<string, IAudioContext>();
  52. const isAudioPlaying = ref<boolean>(false);
  53. onUnmounted(() => {
  54. const audioContext = getAudio();
  55. if (isAudioPlaying.value) {
  56. stopAudio();
  57. }
  58. audioContext?.destroy?.();
  59. audioMap.delete('audio');
  60. });
  61. watch(() => props.broadcastNewAudioSrc, (newSrc) => {
  62. if (newSrc !== props.content.url && isAudioPlaying.value) {
  63. stopAudio();
  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 will be cached, it must be get first
  77. const audioContext = getAudio();
  78. if (!audioContext) {
  79. audioMap.set('audio', uni.createInnerAudioContext() as IAudioContext);
  80. // #ifdef MP
  81. uni.setInnerAudioOption({
  82. obeyMuteSwitch: false,
  83. });
  84. // #endif
  85. initAudioSrc();
  86. }
  87. toggleAudioPlayState();
  88. }
  89. function toggleAudioPlayState() {
  90. if (!isAudioPlaying.value) {
  91. playAudio();
  92. } else {
  93. stopAudio();
  94. }
  95. }
  96. function initAudioSrc() {
  97. const audioContext = getAudio();
  98. if (!audioContext) {
  99. return;
  100. }
  101. audioContext.src = props.content.url;
  102. isAudioPlaying.value = false;
  103. audioContext.onPlay(onAudioPlay);
  104. audioContext.onStop(onAudioStop);
  105. audioContext.onEnded(onAudioEnded);
  106. audioContext.onError(onAudioError);
  107. }
  108. function playAudio() {
  109. const audioContext = getAudio();
  110. if (!audioContext) {
  111. return;
  112. }
  113. audioContext.play();
  114. if (props.messageItem.flow === 'in') {
  115. emits('setAudioPlayed', props.messageItem.ID);
  116. }
  117. }
  118. function stopAudio() {
  119. const audioContext = getAudio();
  120. if (!audioContext) {
  121. return;
  122. }
  123. try {
  124. // The memory of the audiocontext is in memory. But The play instance may have been destroyed.
  125. audioContext.stop();
  126. } catch {
  127. // ignore
  128. }
  129. }
  130. function onAudioPlay() {
  131. isAudioPlaying.value = true;
  132. }
  133. function onAudioStop() {
  134. isAudioPlaying.value = false;
  135. }
  136. function onAudioEnded() {
  137. isAudioPlaying.value = false;
  138. }
  139. function onAudioError() {
  140. console.warn('audio played error');
  141. }
  142. function getAudio(): IAudioContext | undefined {
  143. return audioMap.get('audio');
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. $flow-in-bg-color: #fbfbfb;
  148. $flow-out-bg-color: #dceafd;
  149. :not(not) {
  150. display: flex;
  151. flex-direction: column;
  152. box-sizing: border-box;
  153. min-width: 0;
  154. }
  155. .message-audio {
  156. flex-direction: row;
  157. flex: 0 0 auto;
  158. cursor: pointer;
  159. -webkit-tap-highlight-color: transparent;
  160. overflow: hidden;
  161. .audio-icon-container {
  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>