Vue2 实现消息语音报警功能
下面是一个完整的Vue2消息语音报警实现方案,包含多种语音播报方式和自定义配置。
1. 安装依赖
bash
npm install speakingurl
# 或使用更轻量的方案
npm install vue-speech --save
2. 语音报警组件
核心语音服务类
kotlin
// src/utils/voiceAlert.js
class VoiceAlert {
constructor() {
this.synth = window.speechSynthesis;
this.isSupported = !!this.synth;
this.isSpeaking = false;
this.utterance = null;
this.volume = 1;
this.rate = 1;
this.pitch = 1;
this.voice = null;
this.init();
}
init() {
if (!this.isSupported) {
console.warn('浏览器不支持语音合成API');
return;
}
// 加载可用语音列表
this.loadVoices();
// 语音列表加载事件
speechSynthesis.addEventListener('voiceschanged', () => {
this.loadVoices();
});
}
loadVoices() {
this.voices = this.synth.getVoices();
// 优先选择中文语音
const chineseVoice = this.voices.find(voice =>
voice.lang.includes('zh') || voice.lang.includes('CN')
);
this.voice = chineseVoice || this.voices[0];
}
// 播报警报消息
alert(message, options = {}) {
if (!this.isSupported) return false;
// 停止当前播报
this.stop();
this.utterance = new SpeechSynthesisUtterance(message);
// 设置参数
this.utterance.volume = options.volume || this.volume;
this.utterance.rate = options.rate || this.rate;
this.utterance.pitch = options.pitch || this.pitch;
this.utterance.voice = options.voice || this.voice;
this.utterance.lang = options.lang || 'zh-CN';
// 事件监听
this.utterance.onstart = () => {
this.isSpeaking = true;
options.onStart && options.onStart();
};
this.utterance.onend = () => {
this.isSpeaking = false;
options.onEnd && options.onEnd();
};
this.utterance.onerror = (error) => {
this.isSpeaking = false;
console.error('语音播报错误:', error);
options.onError && options.onError(error);
// 降级方案:尝试使用音频文件
if (options.fallbackAudio) {
this.playAudioFallback(options.fallbackAudio);
}
};
this.synth.speak(this.utterance);
return true;
}
// 播放音频文件降级方案
playAudioFallback(audioUrl) {
const audio = new Audio(audioUrl);
audio.play().catch(error => {
console.error('音频播放失败:', error);
});
}
// 停止播报
stop() {
if (this.isSpeaking) {
this.synth.cancel();
this.isSpeaking = false;
}
}
// 暂停播报
pause() {
if (this.isSpeaking) {
this.synth.pause();
}
}
// 恢复播报
resume() {
if (this.synth.paused) {
this.synth.resume();
}
}
// 设置语音参数
setVoiceParams(params) {
if (params.volume !== undefined) this.volume = params.volume;
if (params.rate !== undefined) this.rate = params.rate;
if (params.pitch !== undefined) this.pitch = params.pitch;
if (params.voice !== undefined) this.voice = params.voice;
}
// 获取可用语音列表
getVoices() {
return this.voices || [];
}
// 检查浏览器支持情况
checkSupport() {
return this.isSupported;
}
}
// 创建单例
const voiceAlert = new VoiceAlert();
export default voiceAlert;
Vue2语音报警组件
xml
<!-- src/components/VoiceAlert.vue -->
<template>
<div class="voice-alert">
<!-- 语音控制面板 -->
<div v-if="showControl" class="voice-control-panel">
<div class="control-item">
<label>音量:</label>
<input
type="range"
min="0" max="1" step="0.1"
v-model="volume"
@change="updateVoiceParams"
>
<span>{{ (volume * 100).toFixed(0) }}%</span>
</div>
<div class="control-item">
<label>语速:</label>
<input
type="range"
min="0.1" max="2" step="0.1"
v-model="rate"
@change="updateVoiceParams"
>
<span>{{ rate }}</span>
</div>
<div class="control-item">
<label>音调:</label>
<input
type="range"
min="0.1" max="2" step="0.1"
v-model="pitch"
@change="updateVoiceParams"
>
<span>{{ pitch }}</span>
</div>
<div class="control-item">
<label>语音:</label>
<select v-model="selectedVoice" @change="updateVoiceParams">
<option
v-for="voice in availableVoices"
:key="voice.name"
:value="voice"
>
{{ voice.name }} ({{ voice.lang }})
</option>
</select>
</div>
</div>
<!-- 报警消息列表 -->
<div class="alert-list">
<div
v-for="alert in recentAlerts"
:key="alert.id"
:class="['alert-item', alert.type]"
>
<div class="alert-content">
<span class="alert-time">{{ formatTime(alert.timestamp) }}</span>
<span class="alert-message">{{ alert.message }}</span>
</div>
<div class="alert-actions">
<button @click="speakAlert(alert)">🔊</button>
<button @click="removeAlert(alert.id)">×</button>
</div>
</div>
</div>
<!-- 控制按钮 -->
<div class="control-buttons">
<button @click="toggleControl">
{{ showControl ? '隐藏设置' : '语音设置' }}
</button>
<button @click="stopSpeaking" :disabled="!isSpeaking">
停止播报
</button>
<button @click="clearAlerts">清空消息</button>
<button @click="testVoice">测试语音</button>
</div>
<!-- 浏览器支持提示 -->
<div v-if="!isSupported" class="browser-warning">
⚠️ 您的浏览器不支持语音合成功能,请使用Chrome、Edge等现代浏览器
</div>
</div>
</template>
<script>
import voiceAlert from '@/utils/voiceAlert'
export default {
name: 'VoiceAlert',
props: {
// 最大保存消息数量
maxAlerts: {
type: Number,
default: 50
},
// 自动播报新消息
autoSpeak: {
type: Boolean,
default: true
},
// 显示控制面板
showControlPanel: {
type: Boolean,
default: false
}
},
data() {
return {
isSupported: false,
isSpeaking: false,
showControl: this.showControlPanel,
volume: 1,
rate: 1,
pitch: 1,
selectedVoice: null,
availableVoices: [],
recentAlerts: [],
alertCounter: 0
}
},
mounted() {
this.isSupported = voiceAlert.checkSupport();
if (this.isSupported) {
this.availableVoices = voiceAlert.getVoices();
this.selectedVoice = this.availableVoices[0] || null;
this.updateVoiceParams();
}
// 监听全局报警消息
this.$eventBus.$on('voice-alert', this.handleNewAlert);
},
beforeDestroy() {
this.$eventBus.$off('voice-alert', this.handleNewAlert);
voiceAlert.stop();
},
methods: {
// 处理新报警消息
handleNewAlert(alertData) {
const alert = {
id: ++this.alertCounter,
message: alertData.message,
type: alertData.type || 'info',
timestamp: new Date(),
priority: alertData.priority || 1
};
// 添加到消息列表
this.recentAlerts.unshift(alert);
// 限制消息数量
if (this.recentAlerts.length > this.maxAlerts) {
this.recentAlerts = this.recentAlerts.slice(0, this.maxAlerts);
}
// 自动播报
if (this.autoSpeak && this.isSupported) {
this.speakAlert(alert);
}
},
// 播报警报消息
speakAlert(alert) {
const message = this.formatAlertMessage(alert);
voiceAlert.alert(message, {
volume: this.volume,
rate: this.rate,
pitch: this.pitch,
voice: this.selectedVoice,
onStart: () => {
this.isSpeaking = true;
},
onEnd: () => {
this.isSpeaking = false;
},
onError: (error) => {
this.isSpeaking = false;
console.error('语音播报失败:', error);
this.$notify.error('语音播报失败');
}
});
},
// 格式化报警消息
formatAlertMessage(alert) {
const prefixes = {
error: '紧急报警:',
warning: '警告:',
info: '通知:',
success: '正常:'
};
const prefix = prefixes[alert.type] || '';
return prefix + alert.message;
},
// 停止播报
stopSpeaking() {
voiceAlert.stop();
this.isSpeaking = false;
},
// 更新语音参数
updateVoiceParams() {
voiceAlert.setVoiceParams({
volume: this.volume,
rate: this.rate,
pitch: this.pitch,
voice: this.selectedVoice
});
},
// 移除报警消息
removeAlert(alertId) {
this.recentAlerts = this.recentAlerts.filter(alert => alert.id !== alertId);
},
// 清空所有消息
clearAlerts() {
this.recentAlerts = [];
},
// 切换控制面板显示
toggleControl() {
this.showControl = !this.showControl;
},
// 测试语音
testVoice() {
this.handleNewAlert({
message: '这是一条测试语音消息,当前语音设置正常。',
type: 'info'
});
},
// 格式化时间
formatTime(timestamp) {
return timestamp.toLocaleTimeString('zh-CN', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
}
}
</script>
<style scoped>
.voice-alert {
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 16px;
background: #fafafa;
max-width: 600px;
margin: 0 auto;
}
.voice-control-panel {
background: white;
padding: 16px;
border-radius: 4px;
margin-bottom: 16px;
border: 1px solid #ddd;
}
.control-item {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.control-item label {
width: 60px;
font-weight: bold;
}
.control-item input[type="range"] {
flex: 1;
margin: 0 8px;
}
.alert-list {
max-height: 300px;
overflow-y: auto;
margin-bottom: 16px;
}
.alert-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 12px;
margin-bottom: 4px;
border-radius: 4px;
background: white;
border-left: 4px solid #ccc;
}
.alert-item.error {
border-left-color: #f44336;
background: #ffebee;
}
.alert-item.warning {
border-left-color: #ff9800;
background: #fff3e0;
}
.alert-item.info {
border-left-color: #2196f3;
background: #e3f2fd;
}
.alert-item.success {
border-left-color: #4caf50;
background: #e8f5e8;
}
.alert-content {
flex: 1;
}
.alert-time {
font-size: 12px;
color: #666;
margin-right: 8px;
}
.alert-actions button {
background: none;
border: none;
cursor: pointer;
margin-left: 4px;
padding: 4px;
}
.control-buttons {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.control-buttons button {
padding: 8px 16px;
border: 1px solid #ddd;
background: white;
border-radius: 4px;
cursor: pointer;
}
.control-buttons button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.browser-warning {
background: #fff3cd;
border: 1px solid #ffeaa7;
color: #856404;
padding: 12px;
border-radius: 4px;
margin-top: 16px;
}
</style>
3. 全局事件总线配置
javascript
// src/utils/eventBus.js
import Vue from 'vue';
// 创建事件总线
export const eventBus = new Vue();
// 在main.js中全局注册
// import { eventBus } from './utils/eventBus';
// Vue.prototype.$eventBus = eventBus;
4. 使用示例
xml
<!-- src/views/Dashboard.vue -->
<template>
<div class="dashboard">
<h1>系统监控面板</h1>
<!-- 语音报警组件 -->
<voice-alert
:auto-speak="true"
:max-alerts="30"
ref="voiceAlert"
/>
<!-- 模拟报警按钮 -->
<div class="demo-buttons">
<button @click="triggerTestAlert('info')">触发信息报警</button>
<button @click="triggerTestAlert('warning')">触发警告报警</button>
<button @click="triggerTestAlert('error')">触发错误报警</button>
<button @click="triggerMQTTAlert">模拟MQTT消息</button>
</div>
<!-- 实时数据展示 -->
<div class="sensor-data">
<div
v-for="sensor in sensorData"
:key="sensor.id"
class="sensor-item"
:class="{ 'alert': sensor.value > sensor.threshold }"
>
<h3>{{ sensor.name }}</h3>
<p>数值: {{ sensor.value }}</p>
<p>状态: {{ sensor.status }}</p>
</div>
</div>
</div>
</template>
<script>
import VoiceAlert from '@/components/VoiceAlert.vue'
export default {
name: 'Dashboard',
components: {
VoiceAlert
},
data() {
return {
sensorData: [
{ id: 1, name: '温度传感器', value: 25, threshold: 30, status: '正常' },
{ id: 2, name: '湿度传感器', value: 60, threshold: 80, status: '正常' },
{ id: 3, name: '压力传感器', value: 100, threshold: 120, status: '正常' }
],
mqttClient: null
}
},
mounted() {
// 模拟MQTT消息接收
this.simulateMQTTMessages();
},
methods: {
// 触发测试报警
triggerTestAlert(type) {
const messages = {
info: '系统运行正常,所有传感器数据在正常范围内。',
warning: '警告:温度传感器数值接近阈值,请关注。',
error: '紧急报警:压力传感器数值超限,立即处理!'
};
this.$eventBus.$emit('voice-alert', {
message: messages[type],
type: type,
priority: type === 'error' ? 3 : type === 'warning' ? 2 : 1
});
},
// 模拟MQTT报警
triggerMQTTAlert() {
const alerts = [
'检测到设备离线:传感器节点002',
'数据异常:温度突然升高5度',
'系统提示:存储空间使用率达到85%',
'网络中断:与服务器连接丢失'
];
const randomAlert = alerts[Math.floor(Math.random() * alerts.length)];
const types = ['info', 'warning', 'error'];
const randomType = types[Math.floor(Math.random() * types.length)];
this.$eventBus.$emit('voice-alert', {
message: randomAlert,
type: randomType,
priority: randomType === 'error' ? 3 : randomType === 'warning' ? 2 : 1
});
},
// 模拟MQTT消息接收
simulateMQTTMessages() {
// 定时模拟传感器数据更新
setInterval(() => {
this.sensorData.forEach(sensor => {
// 模拟数据波动
const change = (Math.random() - 0.5) * 10;
sensor.value = Math.max(0, sensor.value + change);
// 更新状态
if (sensor.value > sensor.threshold) {
sensor.status = '超限';
// 触发语音报警
this.$eventBus.$emit('voice-alert', {
message: `${sensor.name}数值超限,当前值:${sensor.value.toFixed(1)}`,
type: 'error',
priority: 3
});
} else if (sensor.value > sensor.threshold * 0.8) {
sensor.status = '警告';
this.$eventBus.$emit('voice-alert', {
message: `${sensor.name}数值接近阈值,当前值:${sensor.value.toFixed(1)}`,
type: 'warning',
priority: 2
});
} else {
sensor.status = '正常';
}
});
}, 10000); // 每10秒更新一次
}
}
</script>
<style scoped>
.dashboard {
padding: 20px;
}
.demo-buttons {
margin: 20px 0;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.demo-buttons button {
padding: 10px 20px;
border: 1px solid #ddd;
background: #f5f5f5;
border-radius: 4px;
cursor: pointer;
}
.sensor-data {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-top: 20px;
}
.sensor-item {
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
background: white;
}
.sensor-item.alert {
border-color: #f44336;
background: #ffebee;
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.4); }
70% { box-shadow: 0 0 0 10px rgba(244, 67, 54, 0); }
100% { box-shadow: 0 0 0 0 rgba(244, 67, 54, 0); }
}
</style>
5. 在main.js中全局配置
javascript
// src/main.js
import Vue from 'vue'
import App from './App.vue'
import { eventBus } from './utils/eventBus'
// 全局事件总线
Vue.prototype.$eventBus = eventBus
// 全局语音报警方法
Vue.prototype.$voiceAlert = function(message, options = {}) {
eventBus.$emit('voice-alert', {
message,
type: options.type || 'info',
priority: options.priority || 1
});
}
new Vue({
render: h => h(App),
}).$mount('#app')
主要特性
- 多浏览器支持: 基于Web Speech API,支持现代浏览器
- 可配置参数: 音量、语速、音调、语音选择
- 消息管理: 历史消息记录和清理
- 优先级处理: 支持不同级别的报警消息
- 降级方案: 语音合成失败时可使用音频文件
- 易于集成: 可与MQTT、WebSocket等实时消息系统结合
这个实现提供了完整的语音报警功能,可以根据实际需求进行调整和扩展。