js版语音播报
js版语音播报
查看案例>>
根据API播报,为真则连续播放3次,间隔5秒,然后继续与API判断。
查看案例>>
<template>
<view class="container">
<button @click="playSound">测试播放声音 6</button>
<text class="tiptxt">{{ tiptxt }}</text>
<view class="countdown-container">
<text class="countdown-text">{{ countdownTime }}</text>
<button @click="startCountdown">提高音质</button>
<button @click="stopCountdown" :disabled="!isCounting">标准音质</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
innerAudioContext: null, // 内部音频上下文
countdownTime: '00:05', // 初始倒计时时间,假设为5秒
timer: null, // 定时器
isCounting: false, // 是否正在倒计时
currentTime: '', // 存储当前时间的字符串
tiptxt: '', // 提示文本
userid: '10', // 会员ID
soundPlayCount: 0, // 当前播放次数
soundPlayInterval: 5000, // 播放间隔时间,单位毫秒
maxSoundPlays: 3, // 最大播放次数
};
},
onLoad() {
// 创建内部音频上下文
this.innerAudioContext = uni.createInnerAudioContext();
// 设置音频源文件地址
this.innerAudioContext.src = '/Images/sound/android.mp3?v2';
this.getCurrentTime(); // 加载页面时获取当前时间
},
methods: {
startSoundSequence() {
this.soundPlayCount = 0; // 重置播放计数
this.playSoundSequence();
},
playSoundSequence() {
if (this.soundPlayCount < this.maxSoundPlays) {
this.playSound();
this.soundPlayCount++;
this.countdownTime = "当前是第 "+this.soundPlayCount+"/"+ this.maxSoundPlays +" 次播放";
setTimeout(() => {
this.playSoundSequence();
}, this.soundPlayInterval);
}else{
console.log("播放完了")
this.countdownTime = '00:02';
this.startCountdown(); // 重新开始倒计时
}
},
playSound() {
this.innerAudioContext.play();
},
pauseSound() {
this.innerAudioContext.pause();
},
stopSound() {
this.innerAudioContext.stop();
},
startCountdown() {
if (this.isCounting) return; // 如果已经在倒计时,则不执行
let totalSeconds = parseInt(this.countdownTime.split(':')[1]); // 提取秒数
this.isCounting = true;
this.timer = setInterval(() => {
totalSeconds--;
if (totalSeconds < 0) {
clearInterval(this.timer);
this.timer = null;
this.isCounting = false;
uni.showToast({
title: '倒计时结束',
icon: 'none'
});
var app = getApp();
app.globalData.utils.request({
url: '/api/app/1.asp',
data: {
time: this.currentTime,
userid: this.userid
},
success: res => {
console.log('res', res);
//第一次加载时不处理,只记录
if(this.tiptxt==''){
this.tiptxt = res.c;
this.countdownTime = '00:05';
this.startCountdown(); // 重新开始倒计时
}else if (this.tiptxt != res.c) {
this.countdownTime = '00:99';
clearInterval(this.timer);
this.timer = null;
this.isCounting = false;
this.startSoundSequence();//li继续播放3次,间隔3秒
this.tiptxt = res.c;
}else{
this.countdownTime = '00:05';
this.startCountdown(); // 重新开始倒计时
}
},
fail: res => {
console.log('提示', "失败");
}
});
} else {
let minutes = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
this.countdownTime = `${minutes}:${seconds}`;
}
}, 1000); // 每秒更新一次
},
stopCountdown() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
this.isCounting = false;
}
},
getCurrentTime() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
this.currentTime = `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
},
handleInput(e) {
console.log('输入的内容:', e.detail.value);
},
},
};
</script>
<style>
/* 样式 */
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
</style>