diff --git a/pages/im/components/showMessage.vue b/pages/im/components/showMessage.vue
new file mode 100644
index 0000000..48d48ea
--- /dev/null
+++ b/pages/im/components/showMessage.vue
@@ -0,0 +1,8 @@
+
+
+
+
+
+
diff --git a/pages/im/components/showVoice.vue b/pages/im/components/showVoice.vue
index 91b7b3e..0b2ed79 100644
--- a/pages/im/components/showVoice.vue
+++ b/pages/im/components/showVoice.vue
@@ -1,14 +1,14 @@
-
-
- {{ contact(message.senderUserId).name }}
-
-
-
- {{ duration }}"
-
-
-
-
+
+
+ {{ contact(message.senderUserId).name }}
+
+
+
+
+ {{ duration }}"
+
+
+
@@ -29,6 +29,12 @@
default: false
}
},
+ data() {
+ return {
+ onPlay: false,
+ innerAC: null
+ }
+ },
computed: {
duration() {
return this.message.content.duration
@@ -42,6 +48,14 @@
}
}
},
+ mounted() {
+ // 这样能临时解决,但是会一直监听
+ uni.$on('onVoiceMessagePlay', (messageId) => {
+ if (this.message.messageId != messageId) {
+ this.stopPlayVoice()
+ }
+ })
+ },
methods: {
// 播放语音消息
onPlayMsg() {
@@ -62,17 +76,37 @@
}
},
playMsg(path) {
- uni.$emit('onVideoMessagePlay')
- const innerAudioContext = uni.createInnerAudioContext()
- innerAudioContext.src = path
- innerAudioContext.autoplay = true
- innerAudioContext.onStop(resStop => {
- uni.$emit('onVideoMessageStop')
- innerAudioContext.destroy()
+ console.log('准备播放', this.message.content);
+ this.innerAC = uni.createInnerAudioContext()
+ this.innerAC.src = path
+ this.innerAC.autoplay = false
+ this.innerAC.onCanplay(res => {
+ this.innerAC.play()
})
- innerAudioContext.onError(err => {
- console.log(err);
+ this.innerAC.onPlay(res => {
+ this.onPlay = true
+ uni.$emit('onVoiceMessagePlay', this.message.messageId)
})
+ this.innerAC.onEnded(res => {
+ this.innerAC.stop()
+ })
+ this.innerAC.onStop(() => {
+ this.onPlay = false
+ this.innerAC.destroy()
+ this.innerAC = null
+ uni.$emit('onVoiceMessageEnded', this.message.messageId)
+ })
+ this.innerAC.onError(err => {
+ uni.showToast({
+ icon: 'none',
+ title: '语音播放失败'
+ })
+ })
+ },
+ stopPlayVoice() {
+ if (this.innerAC) {
+ this.innerAC.stop()
+ }
}
}
}
@@ -111,6 +145,10 @@
border-radius: 0 20rpx 20rpx 20rpx;
background: white;
+ &.onPlay {
+ background-color: #EEEEEE;
+ }
+
.duration {
color: #333;
font-size: 30rpx;
@@ -120,6 +158,11 @@
&.right {
border-radius: 20rpx 0 20rpx 20rpx;
background: $main-color;
+ flex-direction: row-reverse;
+
+ &.onPlay {
+ background-color: darken($main-color, 10);
+ }
.duration {
color: white;