index.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <view :class="'audio-message ' + (isMine ? 'my-audio' : '')">
  3. <image :src="`${assetsUrl}talk-voice-msg${isPlaying?'-playing':''}.png`" mode="aspectFit" class="audio-icon"></image>
  4. <view class="audio " @click="handlePlayAudioMessage" :style="'width: ' + 120 + 'rpx'">{{ '" ' + message.payload.second }}</view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. audio: {},
  12. assetsUrl:this.$util.assetsUrl,
  13. isPlaying:false
  14. };
  15. },
  16. components: {},
  17. props: {
  18. message: {
  19. type: Object,
  20. default: () => {}
  21. },
  22. isMine: {
  23. type: Boolean,
  24. default: true
  25. }
  26. },
  27. watch: {
  28. message: {
  29. handler: function(newVal) {
  30. this.message=newVal;
  31. // this.setData({
  32. // message: newVal
  33. // });
  34. },
  35. immediate: true,
  36. deep: true
  37. }
  38. },
  39. beforeMount() {
  40. this.audio = uni.createInnerAudioContext();
  41. this.audio.onPlay(() => {});
  42. this.audio.onEnded(() => {
  43. this.isPlaying=false;
  44. });
  45. this.audio.onError(e => {
  46. console.error(e, 'onError');
  47. // ios 音频播放无声,可能是因为系统开启了静音模式
  48. uni.showToast({
  49. icon: 'none',
  50. title: '该音频暂不支持播放'
  51. });
  52. });
  53. },
  54. methods: {
  55. handlePlayAudioMessage() {
  56. this.audio.src = this.message.payload.url;
  57. this.isPlaying=true;
  58. this.audio.play();
  59. }
  60. }
  61. };
  62. </script>
  63. <style>
  64. @import './index.css';
  65. </style>