当前位置: 首页 > news >正文

前端调用阿里云接口语音合成演示

前端页面(html)

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>阿里云语音合成演示</title><style>/* 样式代码保持不变(见原始代码) */</style>
</head>
<body><div class="container"><!-- 页面结构保持不变(见原始代码) --></div><script>document.addEventListener('DOMContentLoaded', () => {const synthesizeBtn = document.getElementById('synthesize');const audioPlayer = document.getElementById('audioPlayer');const statusDiv = document.getElementById('status');synthesizeBtn.addEventListener('click', async () => {// 获取输入参数const params = {appkey: document.getElementById('appkey').value.trim(),token: document.getElementById('token').value.trim(),text: document.getElementById('text').value.trim(),voice: document.getElementById('voice').value,volume: document.getElementById('volume').value,speed: document.getElementById('speed').value};// 参数验证逻辑...synthesizeBtn.disabled = true;showStatus('正在合成语音,请稍候...', 'info');try {// 调用代理服务const audioData = await fetchAudio(params);const audioBlob = new Blob([audioData], { type: 'audio/wav' });audioPlayer.src = URL.createObjectURL(audioBlob);showStatus('语音合成成功!', 'success');} catch (error) {showStatus(`语音合成失败: ${error.message}`, 'error');} finally {synthesizeBtn.disabled = false;}});async function fetchAudio(params) {// 构造代理请求URLconst proxyUrl = 'http://localhost:3000/tts-proxy';const query = new URLSearchParams({...params,text: params.text // 自动编码}).toString();const response = await fetch(`${proxyUrl}?${query}`);if (!response.ok) {const errorText = await response.text();throw new Error(`请求失败: ${response.status} ${errorText}`);}return await response.arrayBuffer();}function showStatus(message, type) {/* 状态显示逻辑 */}});</script>
</body>
</html>

前端直接调用阿里云语音合成API时,通常会遇到跨域请求限制(CORS)。浏览器出于安全考虑会阻止这类跨域请求。

解决方案

搭建Node.js代理服务器作为前端和阿里云API之间的中转层,解决跨域问题。以下是完整实现:

代理服务器实现(Node.js)
const express = require("express");
const axios = require("axios");
const app = express();// 请求日志中间件
app.use((req, res, next) => {console.log(`${new Date().toLocaleString()} - ${req.method} ${req.url}`);next();
});// 处理预检请求
app.options("/tts-proxy", (req, res) => {res.header("Access-Control-Allow-Origin", "*");res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With");res.sendStatus(204);
});// 语音合成代理接口
app.get("/tts-proxy", async (req, res) => {res.header("Access-Control-Allow-Origin", "*");try {const { appkey, token, text, voice, volume, speed } = req.query;const apiUrl = `https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts?appkey=${appkey}&token=${token}&text=${encodeURIComponent(text)}&voice=${voice}&volume=${volume}&speech_rate=${speed}&format=wav`;const response = await axios({method: "post",url: apiUrl,responseType: "arraybuffer",});res.set("Content-Type", "audio/wav");res.send(response.data);} catch (error) {console.error("代理错误:", error);if (error.response) {console.error("阿里云错误状态:", error.response.status);console.error("阿里云错误数据:", error.response.data.toString());}res.status(500).json({error: "语音合成失败",details: error.message,});}
});// 启动服务
const PORT = 3000;
app.listen(PORT, () => {console.log(`代理服务运行在 http://localhost:${PORT}`);
});

实现原理

  1. 跨域处理

    • 代理服务器设置CORS响应头(Access-Control-Allow-Origin: *
    • 处理OPTIONS预检请求
  2. 请求转发

    • 前端调用本地代理接口(http://localhost:3000/tts-proxy)
    • 代理服务器转发请求到阿里云API
    • 代理服务器将音频数据返回给前端
  3. 错误处理

    • 捕获代理服务和阿里云API的错误
    • 返回详细错误信息给前端
       

使用步骤

  1. 安装依赖:
    npm install express axios

  2. 启动代理服务:
    node server.js

  3. 访问前端页面:
    http://localhost:3000/index.html
  4. 输入参数并合成语音
http://www.dtcms.com/a/341526.html

相关文章:

  • 20人团队文件共享选哪款?群晖DS925+ 和 DS1525+深度对比
  • 反射基础知识初入(up晚上回家再补完剩下的)
  • Anomalib:在Linux服务器上安装使用Anomalib 2.1.0
  • 生意参谋-市场竞争分析-提升商品成长效率
  • PostgreSQL 中的金钱计算处理
  • C语言第十章内存函数
  • 《SQLAlchemy 2 In Practice》读后感
  • win与ubuntu双系统安装笔记
  • 小波函数多尺度变换的 Curvelet 变换
  • vue3项目,使用vue2方式来写,可以吗
  • 【嵌入式】CAN通信
  • 基于XGBoost算法的数据回归预测 极限梯度提升算法 XGBoost
  • 虚拟机部署HDFS集群
  • JDK 工具
  • IDEA(十四) IntelliJ Idea 常用快捷键(Mac)
  • 会计人员职业发展框架:核心能力构建与进阶路径
  • ROADS落地的架构蓝图
  • Java 通过 m3u8 链接下载所有 ts 视频切片并合并转换为 mp4 格式
  • Odoo 18 通用图片导入工具:从零到一的企业级开发实战
  • 记录一次ubuntu系统下ovito无法调用显卡驱动报错
  • keepalived的配置
  • Java内置注解
  • 区块链技术:重塑未来互联网的伟大动力
  • 中金所股指期货交易规则
  • c++之指针和引用
  • 第三十三天(信号量)
  • 大模型—— DeepSeek V3.1 Base / Instruct 发布
  • Mqtt — 使用详解EMQX,MQTTX
  • Annexin V应用指南--多领域应用与实验陷阱规避
  • MySQL之分区功能