message-audio.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view class="message-audio" :class="['content content-' + message.flow]" @click="handlePlay">
  3. <image v-if="message.flow==='in'" class="audio-icon audio-icon-in" src="../../../../assets/icon/audio-play.svg"></image>
  4. <view>{{data.second}}s</view>
  5. <image v-if="message.flow==='out'" class="audio-icon " src="../../../../assets/icon/audio-play.svg"></image>
  6. <!-- <view class="play-icon" v-if="message.flow==='in' && !isPlay" ></view>
  7. -->
  8. </view>
  9. </template>
  10. <script lang="ts">
  11. import { defineComponent, watchEffect, reactive, toRefs, ref, onMounted } from 'vue';
  12. const audio = uni.createInnerAudioContext();
  13. const MessageAudio = defineComponent({
  14. props: {
  15. data: {
  16. type: Object,
  17. default: () => {
  18. return {};
  19. }
  20. },
  21. messageData: {
  22. type: Object,
  23. default: () => {
  24. return {};
  25. }
  26. }
  27. },
  28. setup(props:any, ctx:any) {
  29. const data = reactive({
  30. data: {},
  31. message: {},
  32. isPlay: false,
  33. });
  34. watchEffect(()=>{
  35. data.data = props.data;
  36. data.message = props.messageData;
  37. });
  38. onMounted(() => {
  39. audio.onPlay(() => {
  40. console.log('开始播放');
  41. });
  42. audio.onEnded(() => {
  43. console.log('停止播放');
  44. });
  45. audio.onError(e => {
  46. console.error(e, 'onError');
  47. // ios 音频播放无声,可能是因为系统开启了静音模式
  48. // uni.showToast({
  49. // icon: 'none',
  50. // title: '该音频暂不支持播放'
  51. // });
  52. });
  53. })
  54. const handlePlay = () => {
  55. if (data.data.url) {
  56. audio.src = data.data.url;
  57. audio.play();
  58. }
  59. data.isPlay = true
  60. };
  61. return {
  62. ...toRefs(data),
  63. audio,
  64. handlePlay
  65. };
  66. }
  67. });
  68. export default MessageAudio
  69. </script>
  70. <style lang="scss" scoped>
  71. .message-audio {
  72. display: flex;
  73. width: 80px;
  74. align-items: center;
  75. position: relative;
  76. .icon {
  77. margin: 0 4px;
  78. }
  79. .audio-icon {
  80. width: 20px;
  81. height: 20px;
  82. padding: 0 5px;
  83. }
  84. .audio-icon-in {
  85. transform: rotate(180deg)
  86. }
  87. .play-icon {
  88. position: absolute;
  89. right: -7px;
  90. top: 1px;
  91. width: 6px;
  92. height: 6px;
  93. border-radius: 100%;
  94. background-color: #f73232;
  95. }
  96. audio {
  97. width: 0;
  98. height: 0;
  99. }
  100. }
  101. .reserve {
  102. flex-direction: row-reverse;
  103. }
  104. .mask {
  105. position: fixed;
  106. width: 100vw;
  107. height: 100vh;
  108. top: 0;
  109. left: 0;
  110. opacity: 0;
  111. z-index: 1;
  112. }
  113. .content {
  114. padding: 10px;
  115. &-in {
  116. background: #e1e1e1;
  117. border-radius: 0px 10px 10px 10px;
  118. }
  119. &-out {
  120. background: #DCEAFD;
  121. border-radius: 10px 0px 10px 10px;
  122. }
  123. }
  124. </style>