This commit is contained in:
shengchanzhe 2023-10-12 20:13:40 +08:00
parent 92dfbc3411
commit 0b60385d73
1 changed files with 42 additions and 25 deletions

View File

@ -1,5 +1,5 @@
<script setup lang='ts'> <script setup lang='ts'>
import { computed, reactive, ref } from 'vue' import { computed, reactive, ref, watch } from 'vue'
import { NDropdown, useMessage } from 'naive-ui' import { NDropdown, useMessage } from 'naive-ui'
import AvatarComponent from './Avatar.vue' import AvatarComponent from './Avatar.vue'
import TextComponent from './Text.vue' import TextComponent from './Text.vue'
@ -92,18 +92,18 @@ async function handleCopy() {
} }
} }
let playIndex = 0; let playing = 0
let playing = false;
async function radioPlay() { async function radioPlay() {
console.log('播放', props.mp3) console.log('播放', props.mp3)
const socket = new WebSocket('wss://chat.lihaink.cn/zhanti/tts'); audioElements = []
const socket = new WebSocket('wss://chat.lihaink.cn/zhanti/tts')
const promise = () => { const promise = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// WebSocket // WebSocket
socket.onopen = () => { socket.onopen = () => {
console.log('socket已连接') console.log('socket已连接')
playIndex = 0; playing = 0
resolve(null) resolve(null)
} }
}) })
@ -117,19 +117,21 @@ async function radioPlay() {
} }
socket.send(JSON.stringify({ socket.send(JSON.stringify({
     "data": "快科技10月12日消息。不宣而发的华为Mate 60在上架官方商城后。直到现在都处于一机难求的状态。由于Mate 60系列的爆火。华为也是将明年的预计手机出货量翻倍到了7000万台。" data: '快。科。技0月12。日消息。不宣而发的。华为Mate 60在上架官方商城后。直到现在都处于一机难求的状态。由于Mate 60系列的爆火。华为也是将明年的预计手机出货量翻倍到了7000万台。',
})) }))
let i = 0
// WebSocket // WebSocket
socket.onmessage = (event: any) => { socket.onmessage = (event: any) => {
const msg = JSON.parse(event.data); const msg = JSON.parse(event.data)
console.log(msg.mp3); console.log(i, msg.mp3)
const a = new Audio(msg.mp3) const a = new Audio(msg.mp3)
a.addEventListener('ended', () => { a.addEventListener('ended', () => {
onAudioEnd(playIndex) onAudioEnd()
}) })
audioElements.push(a) audioElements.push(a)
if(playIndex==0||playing==false) playAudio(playIndex) if (i == 0)
playAudio(i)
i++
} }
// for (let i = 0; i < props.mp3.length; i++) { // for (let i = 0; i < props.mp3.length; i++) {
@ -143,22 +145,29 @@ async function radioPlay() {
// playAudio() // playAudio()
} }
let playFlag = false
const onAudioEnd = (index: any) => { const onAudioEnd = (index: any) => {
if (index + 1 < audioElements.length&&playing==false){ console.log('ok', playing)
audioElements[index + 1].play() playing++
playing = true; playFlag = false
}else if(index + 1 < audioElements.length&&playing==true){ if (playing < audioElements.length) {
playing = true; playFlag = true
}else playing = false; audioElements[playing].play()
}
} }
// //
const audioElements = reactive([]) const audioElements = reactive([])
// //
const playAudio = (playIndex: any) => { const playAudio = (playIndex: any) => {
// for (let i = 0; i < audioElements.length; i++) // for (let i = 0; i < audioElements.length; i++)
audioElements[playIndex].play() audioElements[0].play()
} }
watch(() => audioElements.length, (newVal, oldVal) => {
if (newVal > playing && playFlag == false && newVal > 0)
audioElements[playing].play()
})
// //
const pauseAudio = () => { const pauseAudio = () => {
for (let i = 0; i < audioElements.length; i++) for (let i = 0; i < audioElements.length; i++)
@ -203,6 +212,14 @@ const pauseAudio = () => {
播放 播放
<!-- <SvgIcon icon="ri:restart-line" /> --> <!-- <SvgIcon icon="ri:restart-line" /> -->
</button> </button>
<button
v-if="!inversion"
class="mr-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"
@click="pauseAudio"
>
暂停
<!-- <SvgIcon icon="ri:restart-line" /> -->
</button>
<button <button
v-if="!inversion" v-if="!inversion"
class="mr-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300" class="mr-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"