本地发出的语音播放优化,必须从远端下载到本地之后再播放就好了,版本号升级,发布demo

This commit is contained in:
2022-02-22 17:20:51 +08:00
parent 5a52bdf2dd
commit 17e443fdeb
2 changed files with 63 additions and 58 deletions

View File

@@ -2,8 +2,8 @@
"name" : "ZH-HEALTH", "name" : "ZH-HEALTH",
"appid" : "__UNI__C29473D", "appid" : "__UNI__C29473D",
"description" : "ZH-HEALTH您手上的健康管理专家", "description" : "ZH-HEALTH您手上的健康管理专家",
"versionName" : "1.0.11", "versionName" : "1.0.13",
"versionCode" : 111, "versionCode" : 113,
"transformPx" : false, "transformPx" : false,
/* 5+App */ /* 5+App */
"app-plus" : { "app-plus" : {

View File

@@ -2,9 +2,12 @@
<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}]" :style="{width: width + 'rpx'}" @click="startPlay"> <view :class="['voice', isRemote ? 'left': 'right', {'onPlay': onPlay}]" :style="{width: width + 'rpx'}"
<image v-if="isRemote" class="icon" :class="{ 'videoFlicker' : onPlay && msgId == message.messageId }" src="@/static/icon/audio_green.png" mode="widthFix"></image> @click="startPlay">
<image v-else class="icon" :class="{ 'videoFlicker' : onPlay && msgId == message.messageId }" src="@/static/icon/audio_white.png" mode="widthFix"></image> <image v-if="isRemote" class="icon" :class="{ 'videoFlicker' : onPlay && msgId == message.messageId }"
src="@/static/icon/audio_green.png" mode="widthFix"></image>
<image v-else class="icon" :class="{ 'videoFlicker' : onPlay && msgId == message.messageId }"
src="@/static/icon/audio_white.png" mode="widthFix"></image>
<text class="duration">{{ duration }}"</text> <text class="duration">{{ duration }}"</text>
</view> </view>
<u-badge isDot :show="message.content.local =='' && isRemote" /> <u-badge isDot :show="message.content.local =='' && isRemote" />
@@ -27,13 +30,13 @@
isGroup: { isGroup: {
type: Boolean, type: Boolean,
default: false default: false
} }
}, },
data() { data() {
return { return {
onPlay: false, onPlay: false,
innerAC: null, innerAC: null,
msgId : null msgId: null
} }
}, },
computed: { computed: {
@@ -47,13 +50,13 @@
return function(targetId) { return function(targetId) {
return this.$store.getters.contactInfo(targetId) return this.$store.getters.contactInfo(targetId)
} }
}, },
width() { width() {
if (this.duration > 3) { if (this.duration > 3) {
return (this.duration * 5) + 150 return (this.duration * 5) + 150
} else { } else {
return 120 return 120
} }
} }
}, },
mounted() { mounted() {
@@ -63,12 +66,12 @@
this.stopPlay() this.stopPlay()
} }
}) })
}, },
created() { created() {
console.log(this.message.messageId) console.log(this.message.messageId)
}, },
methods: { methods: {
// 播放语音消息 // 播放语音消息
startPlay() { startPlay() {
@@ -76,9 +79,10 @@
if (this.onPlay) { if (this.onPlay) {
this.stopPlay() this.stopPlay()
return return
} }
this.onPlay = true
// 如果下载到了本地,那么直接播放,否则调用下载语音消息接口,下载后再播放 // 如果下载到了本地,那么直接播放,否则调用下载语音消息接口,下载后再播放
if (this.message.content.local) { if (this.message.content.local && this.message.content.local.indexOf('///data/user/') < 0) {
this.playVoice(this.message.content.local) this.playVoice(this.message.content.local)
} else { } else {
RongIMLib.downloadMediaMessage(this.message.messageId, { RongIMLib.downloadMediaMessage(this.message.messageId, {
@@ -88,27 +92,27 @@
}, },
progress: (progress, messageId) => {}, progress: (progress, messageId) => {},
cancel: (messageId) => {}, cancel: (messageId) => {},
error: (errorCode, messageId) => { error: (errorCode, messageId) => {
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '语音播放失败' title: '语音播放失败'
}) })
} }
}) })
} }
}, },
playVoice(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
this.innerAC.autoplay = false this.innerAC.autoplay = false
this.msgId = this.message.messageId this.msgId = this.message.messageId
this.innerAC.onCanplay(res => { this.innerAC.onCanplay(res => {
console.log('onCanplay')
this.innerAC.play() this.innerAC.play()
}) })
this.innerAC.onPlay(res => {
this.innerAC.onPlay(res => { console.log('onPlay')
this.onPlay = true
uni.$emit('onVoiceMessagePlay', this.message.messageId) uni.$emit('onVoiceMessagePlay', this.message.messageId)
}) })
this.innerAC.onEnded(res => { this.innerAC.onEnded(res => {
@@ -117,11 +121,11 @@
this.innerAC.onStop(() => { this.innerAC.onStop(() => {
this.onPlay = false this.onPlay = false
this.innerAC.destroy() this.innerAC.destroy()
this.innerAC = null this.innerAC = null
this.msgId = null this.msgId = null
uni.$emit('onVoiceMessageStop', this.message.messageId) uni.$emit('onVoiceMessageStop', this.message.messageId)
}) })
this.innerAC.onError(err => { this.innerAC.onError(err => {
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '语音播放失败' title: '语音播放失败'
@@ -129,31 +133,32 @@
}) })
}, },
stopPlay() { stopPlay() {
// 停止播放语音消息
if (this.innerAC) { if (this.innerAC) {
this.innerAC.stop() this.innerAC.stop()
} }
} }
} }
} }
</script> </script>
<style> <style>
@keyframes playFlicker{ @keyframes playFlicker {
0%{ 0% {
opacity: 1; opacity: 1;
} }
50%{
opacity: .5; 50% {
} opacity: .5;
100%{ }
opacity: 1;
} 100% {
} opacity: 1;
}
.videoFlicker{ }
animation: playFlicker 1s infinite;
} .videoFlicker {
animation: playFlicker 1s infinite;
}
</style> </style>
<style scoped lang="scss"> <style scoped lang="scss">