index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view
  3. :class="[
  4. 'audio_msg_container',
  5. isSelf ? 'audio_msg_container_mine' : 'audio_msg_container_other',
  6. ]"
  7. @click="playAudioMessage"
  8. >
  9. <view
  10. :class="[
  11. 'audio_msg_play_icon',
  12. isSelf ? 'audio_msg_play_icon_mine' : 'audio_msg_play_icon_other',
  13. playAudioMid == msgBody.id
  14. ? isSelf
  15. ? 'start_mine_play_audio'
  16. : 'start_other_play_audio'
  17. : '',
  18. ]"
  19. >
  20. </view>
  21. <view class="audio_msg_length">{{ audioLength }}''</view>
  22. </view>
  23. </template>
  24. <script>
  25. import { EMClient } from '@/EaseIM';
  26. const loginAccessToken = EMClient.token;
  27. export default {
  28. props: {
  29. msgBody: {
  30. type: Object,
  31. default: () => ({}),
  32. },
  33. playAudioMid: {
  34. type: String,
  35. default: '',
  36. },
  37. },
  38. data() {
  39. return {
  40. innerAudioContext: null,
  41. isPlaying: false,
  42. };
  43. },
  44. computed: {
  45. isSelf() {
  46. const { from } = this.msgBody;
  47. return (
  48. from === this.$store.state.LoginStore.loginUserBaseInfos.loginUserId
  49. );
  50. },
  51. audioLength() {
  52. const { length, body } = this.msgBody;
  53. return length || body?.length;
  54. },
  55. },
  56. mounted() {
  57. this.innerAudioContext = uni.createInnerAudioContext({
  58. obeyMuteSwitch: false,
  59. });
  60. this.innerAudioContext.onPlay(() => {
  61. console.log('>>>>>播放中');
  62. });
  63. this.innerAudioContext.onEnded(() => {
  64. this.$emit('onClickPlayAudio', '');
  65. });
  66. this.innerAudioContext.onError((res) => {
  67. console.log(res.errMsg);
  68. uni.showToast({ title: '播放失败', icon: 'none' });
  69. });
  70. },
  71. methods: {
  72. //转换为mp3格式
  73. formatAudioToMp3() {
  74. const { url, body } = this.msgBody;
  75. uni.downloadFile({
  76. url: url || body?.url,
  77. header: {
  78. 'X-Requested-With': 'XMLHttpRequest',
  79. Accept: 'audio/mp3',
  80. Authorization: 'Bearer ' + loginAccessToken,
  81. },
  82. success: (res) => {
  83. console.log('>>>>>成功拿到播放地址', res);
  84. const tempFilePath = res.tempFilePath;
  85. this.innerAudioContext.src = tempFilePath;
  86. console.log(this.innerAudioContext.src);
  87. this.innerAudioContext.play();
  88. },
  89. fail: (e) => {
  90. console.log('downloadFile failed', e);
  91. uni.showToast({
  92. title: '语音转mp3失败',
  93. duration: 2000,
  94. });
  95. },
  96. });
  97. },
  98. playAudioMessage() {
  99. const { id } = this.msgBody;
  100. this.formatAudioToMp3();
  101. console.log('>>>>>通知进行播放');
  102. this.$emit('onClickPlayAudio', id);
  103. },
  104. },
  105. beforeDestroy() {
  106. this.innerAudioContext && this.innerAudioContext.destroy();
  107. },
  108. };
  109. </script>
  110. <style scoped>
  111. @import './index.css';
  112. </style>