index.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <view :class="'audio-message ' + (isMine ? 'my-audio' : '')">
  3. <image class="audio-icon" src="../../../../../static/images/audio.png"></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. };
  13. },
  14. components: {},
  15. props: {
  16. message: {
  17. type: Object,
  18. default: () => {}
  19. },
  20. isMine: {
  21. type: Boolean,
  22. default: true
  23. }
  24. },
  25. watch: {
  26. message: {
  27. handler: function(newVal) {
  28. this.setData({
  29. message: newVal
  30. });
  31. },
  32. immediate: true,
  33. deep: true
  34. }
  35. },
  36. beforeMount() {
  37. this.audio = uni.createInnerAudioContext();
  38. this.audio.onPlay(() => {
  39. console.log('开始播放');
  40. });
  41. this.audio.onEnded(() => {
  42. console.log('停止播放');
  43. });
  44. this.audio.onError(e => {
  45. console.error(e, 'onError');
  46. // ios 音频播放无声,可能是因为系统开启了静音模式
  47. uni.showToast({
  48. icon: 'none',
  49. title: '该音频暂不支持播放'
  50. });
  51. });
  52. },
  53. methods: {
  54. handlePlayAudioMessage() {
  55. this.audio.src = this.message.payload.url;
  56. this.audio.play();
  57. }
  58. }
  59. };
  60. </script>
  61. <style>
  62. @import './index.css';
  63. </style>