语音消息条,长度优化

This commit is contained in:
2022-02-22 11:27:28 +08:00
parent a396f75041
commit d812ac2862

View File

@@ -2,7 +2,7 @@
<view class=""> <view class="">
<view class="name" v-if="isGroup && isRemote">{{ contact(message.senderUserId).name }}</view> <view class="name" v-if="isGroup && isRemote">{{ contact(message.senderUserId).name }}</view>
<view class="msg--voice"> <view class="msg--voice">
<view :class="['voice', isRemote ? 'left': 'right', {'onPlay': onPlay}]" @click="onPlayMsg"> <view :class="['voice', isRemote ? 'left': 'right', {'onPlay': onPlay}]" :style="{width: width + 'rpx'}" @click="startPlay">
<image v-if="isRemote" class="icon" src="@/static/icon/audio_green.png" mode="widthFix"></image> <image v-if="isRemote" class="icon" src="@/static/icon/audio_green.png" mode="widthFix"></image>
<image v-else class="icon" src="@/static/icon/audio_white.png" mode="widthFix"></image> <image v-else class="icon" src="@/static/icon/audio_white.png" mode="widthFix"></image>
<text class="duration">{{ duration }}"</text> <text class="duration">{{ duration }}"</text>
@@ -32,7 +32,7 @@
data() { data() {
return { return {
onPlay: false, onPlay: false,
innerAC: null innerAC: null,
} }
}, },
computed: { computed: {
@@ -46,36 +46,52 @@
return function(targetId) { return function(targetId) {
return this.$store.getters.contactInfo(targetId) return this.$store.getters.contactInfo(targetId)
} }
},
width() {
if (this.duration > 3) {
return (this.duration * 5) + 150
} else {
return 120
}
} }
}, },
mounted() { mounted() {
// 这样能临时解决,但是会一直监听 // 这样能临时解决,但是会一直监听
uni.$on('onVoiceMessagePlay', (messageId) => { uni.$on('onVoiceMessagePlay', (messageId) => {
if (this.message.messageId != messageId) { if (this.message.messageId != messageId) {
this.stopPlayVoice() this.stopPlay()
} }
}) })
}, },
methods: { methods: {
// 播放语音消息 // 播放语音消息
onPlayMsg() { startPlay() {
// 如果是正在播放的,停止当前播放
if (this.onPlay) {
this.stopPlay()
return
}
// 如果下载到了本地,那么直接播放,否则调用下载语音消息接口,下载后再播放
if (this.message.content.local) { if (this.message.content.local) {
this.playMsg(this.message.content.local) this.playVoice(this.message.content.local)
} else { } else {
RongIMLib.downloadMediaMessage(this.message.messageId, { RongIMLib.downloadMediaMessage(this.message.messageId, {
success: (path) => { success: (path) => {
this.message.content.local = path this.message.content.local = path
this.playMsg(path) this.playVoice(path)
}, },
progress: (progress, messageId) => {}, progress: (progress, messageId) => {},
cancel: (messageId) => {}, cancel: (messageId) => {},
error: (errorCode, messageId) => { error: (errorCode, messageId) => {
// 发送错误回调 uni.showToast({
icon: 'none',
title: '语音播放失败'
})
} }
}) })
} }
}, },
playMsg(path) { playVoice(path) {
console.log('准备播放', this.message.content); console.log('准备播放', this.message.content);
this.innerAC = uni.createInnerAudioContext() this.innerAC = uni.createInnerAudioContext()
this.innerAC.src = path this.innerAC.src = path
@@ -94,7 +110,7 @@
this.onPlay = false this.onPlay = false
this.innerAC.destroy() this.innerAC.destroy()
this.innerAC = null this.innerAC = null
uni.$emit('onVoiceMessageEnded', this.message.messageId) uni.$emit('onVoiceMessageStop', this.message.messageId)
}) })
this.innerAC.onError(err => { this.innerAC.onError(err => {
uni.showToast({ uni.showToast({
@@ -103,7 +119,8 @@
}) })
}) })
}, },
stopPlayVoice() { stopPlay() {
// 停止播放语音消息
if (this.innerAC) { if (this.innerAC) {
this.innerAC.stop() this.innerAC.stop()
} }
@@ -131,9 +148,8 @@
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
height: 79rpx; height: 84rpx;
width: 170rpx; padding: 0 30rpx;
padding: 0 20rpx;
box-sizing: border-box; box-sizing: border-box;
.icon { .icon {