Files
tra-app/src/pages/sparring/components/question.vue
T

352 lines
7.8 KiB
Vue

<template>
<view class="talking-info" v-if="dataInfo.intrctWay === '02'">
<view :class="['talking-gif', !voiceLoading&&(props.activeVoiceId === dataInfo.id) ? 'is-play' : '']">
<CommonLoading color="#fff" v-show="voiceLoading" />
</view>
<view class="right-time">{{voiceTime}}</view>
<view class="talking-cont">
<text :class="['talking-text translate-text loading']" v-show="!translateLoading">
{{ showContent }}
</text>
<view class="talking-text translate-text" v-if="translateLoading">
<CommonLoading color="#fff" />
</view>
</view>
<view class="btns-cont">
<view class="play-btn">
<image class="icon" v-show="!(!voiceLoading&&(props.activeVoiceId === dataInfo.id))" @click.stop="playVoice"
src="@/static/images/course/play-blue.png" style="width: 100%;height: 100%;"></image>
<image class="icon" v-show="!voiceLoading&&(props.activeVoiceId === dataInfo.id)" @click.stop="pauseVoice"
src="@/static/images/course/stop-blue.png" style="width: 100%;height: 100%;"></image>
<!-- <view class="icon text-btn" v-if="type === 1" @click.stop="translateVoice(dataInfo)"></view> -->
</view>
</view>
</view>
<!-- <view class="talking-info talking-info-text" v-else-if="dataInfo.intrctWay === '01'">
<view class="talking-cont">
<text :class="['talking-text translate-text loading']">
{{ showContent }}
</text>
</view>
</view> -->
<view class="talking-info talking-info-loading" v-else>
<view :class="['talking-gif']">
<CommonLoading color="#fff" />
</view>
</view>
</template>
<script setup>
import {
computed,
unref,
ref,
toRefs,
watch,
onMounted
} from "vue"
import {
onUnload
} from '@dcloudio/uni-app';
import {
getPhoneEnvBool
} from '@/common/common.js';
const props = defineProps({
dataInfo: {
default: () => ({}),
type: Object
},
activeVoiceId: {
default: '',
type: String
},
})
const {
activeVoiceId,
dataInfo
} = toRefs(props)
const emit = defineEmits(['changePlay'])
// 声音播放初始化
const talkUserVoiceObj = ref(null)
const isPlaying = ref(false)
const voiceLoading = ref(false)
const translateLoading = ref(false)
const voiceTime = ref('')
const itemVoice = ref('')
const showContent = computed(() => {
return dataInfo.value.talkingText || ''
})
onMounted(() => {
initVoice()
})
onUnload(() => {
pauseVoice('destory')
})
// 重播
const restartPlay = () => {
if (itemVoice.value) {
emit('changePlay', '');
emit('changePlay', unref(dataInfo)?.id);
setTimeout(() => {
talkUserVoiceObj.value.src = itemVoice.value;
talkUserVoiceObj.value.seek(0);
talkUserVoiceObj.value.play();
}, 100);
} else {
playVoice();
}
};
const pauseVoice = (flag) => {
if (!talkUserVoiceObj.value) return
talkUserVoiceObj.value.pause()
if(flag === 'destory') talkVoiceObj.value.destory()
isPlaying.value = false
emit('changePlay', '')
}
const playVoice = () => {
emit('changePlay', unref(dataInfo)?.id)
talkUserVoiceObj.value.volume = 1
if(isTesting.value){
restartPlay()
return
}
if (talkUserVoiceObj.value && talkUserVoiceObj.value.src) {
setTimeout(() => {
talkUserVoiceObj.value.src = itemVoice.value
talkUserVoiceObj.value.play()
isPlaying.value = true
}, 300)
return
} else {
talkUserVoiceObj.value.src = ''
if (unref(dataInfo).talkingVoice) {
setTimeout(() => {
talkUserVoiceObj.value.src = itemVoice.value
talkUserVoiceObj.value.play()
isPlaying.value = true
}, 100)
}
}
}
const IsAndEnv = getPhoneEnvBool('AND');
const initVoice = () => {
talkUserVoiceObj.value = uni.createInnerAudioContext()
talkUserVoiceObj.value.onPause(() => {
talkUserVoiceObj.value.volume = 1
})
talkUserVoiceObj.value.onPlay(() => {
if (isTesting.value) {
talkUserVoiceObj.value.pause()
isTesting.value = false
}
})
talkUserVoiceObj.value.onEnded(() => {
emit('changePlay', '')
talkUserVoiceObj.value.src = ''
talkUserVoiceObj.value.volume = 1
})
talkUserVoiceObj.value.onError(() => {
isTesting.value = false
talkUserVoiceObj.value.volume = 1
talkUserVoiceObj.value.src = ''
})
talkUserVoiceObj.value.onCanplay(() => {
setVoiceTime(0)
})
itemVoice.value = unref(dataInfo).talkingVoice
// talkUserVoiceObj.value.src = itemVoice.value
// talkUserVoiceObj.value.sessionCategory = 'soloAmbient'
// //#ifdef APP-PLUS
// // 安卓端 须播放一次,不然再次播放会没有声音
// if (IsAndEnv) {
// isTesting.value = true
// talkUserVoiceObj.value.volume = 0
// talkUserVoiceObj.value.play()
// }
// // #endif
}
const isTesting = ref(false)
const setVoiceTime = (sum) => {
let _sum = sum + 1
if (Math.ceil(talkUserVoiceObj.value.duration) > 0) {
voiceTime.value = (Math.ceil(talkUserVoiceObj.value.duration) || 1) + 's'
} else {
if (_sum === 25) {
voiceTime.value = (props.dataInfo.voiceTime|| 1) + 's'
return
}
setTimeout(() => {
setVoiceTime(_sum)
}, 200)
}
}
watch(() => props.dataInfo, () => {}, {
deep: true,
immediate: true
})
watch(() => props.dataInfo.talkingVoice, (val) => {
if (val) {
initVoice()
}
}, {
deep: true,
immediate: true
})
watch(
() => props.activeVoiceId,
(val) => {
if (val !== props.dataInfo.id) {
if (isPlaying.value) {
isPlaying.value = false
if (!talkUserVoiceObj.value) return
talkUserVoiceObj.value.pause()
}
}
}, {
deep: true,
immediate: true
}
)
</script>
<style scoped lang="scss">
.talking-info {
padding: 20rpx;
border-radius: 15rpx;
background-color: #06f;
padding-bottom: 30rpx;
position: relative;
color: #fff;
width: 100%;
float: right;
.talking-gif {
width: 200rpx;
height: 38rpx;
background: url(@/static/images/course/voice-gray.png) no-repeat;
background-size: contain;
padding-left: 168rpx;
&.is-play {
background: url(@/static/images/course/voice-white.gif) no-repeat;
background-size: contain;
}
}
.right-time {
position: absolute;
right: 20rpx;
top: 20rpx;
font-size: 28rpx;
height: 38rpx;
line-height: 38rpx;
}
&.talking-info-loading {
padding-bottom: 20rpx;
}
&.talking-info-text {
padding-bottom: 20rpx;
width: auto;
float: right;
.talking-cont {
margin-top: 0rpx;
}
}
.talking-cont {
margin-top: 16rpx;
overflow: hidden;
.talking-text {
font-size: 28rpx;
line-height: 48rpx;
margin-bottom: 20rpx;
word-break: break-all;
white-space: pre-line;
overflow: hidden;
&.translate-text {
min-height: 48rpx;
}
}
}
.btns-cont {
overflow: hidden;
border-top: 3rpx solid #eee;
.replay-btn {
float: left;
width: 46rpx;
height: 46rpx;
overflow: hidden;
margin-right: 16rpx;
margin-top: 25rpx;
}
.play-btn {
float: left;
width: 46rpx;
height: 46rpx;
overflow: hidden;
margin-right: 16rpx;
margin-top: 25rpx;
}
.text-btn {
float: left;
width: 46rpx;
height: 46rpx;
overflow: hidden;
line-height: 46rpx;
text-align: center;
color: #fff;
margin-right: 16rpx;
margin-top: 25rpx;
background-color: #67A4ff;
font-size: 24rpx;
font-weight: bold;
border-radius: 20rpx;
}
.text-right-btn {
float: right;
padding: 0 10rpx;
height: 46rpx;
overflow: hidden;
line-height: 46rpx;
text-align: center;
margin-left: 16rpx;
margin-top: 25rpx;
}
.next-btn {
float: right;
padding: 0 60rpx 0 20rpx;
height: 62rpx;
line-height: 62rpx;
font-size: 30rpx;
background-color: #06f;
color: #fff;
border-radius: 8rpx;
margin-top: 16rpx;
position: relative;
.icon-iamge {
width: 25rpx;
height: 25rpx;
position: absolute;
right: 20rpx;
top: 50%;
margin-top: -12rpx;
}
}
}
}
</style>