当前位置:网站首页>百度语音合成语音文件并在网站中展示
百度语音合成语音文件并在网站中展示
2022-06-24 20:55:00 【繁华pk落幕】
项目背景:
百度语音合成api
pbootcms(sqlite3版本),mysql或者其他网站可以适当的修改一下流程也可以
数据库操作文件SQLite.php
<?php
//sqlite路径
define("dbfile","../data/106aa56d9c6bda96a8e0829ace2466a6.db");
class SQLiteDB extends SQLite3
{
function __construct($file)
{
$this->open($file);
}
}
//根据sql查询表
function query($db,$sql){
$ret = $db->query($sql);
return $ret;
}
function close($db){
$db->close();
}
//根据id获取数据备注信息
function getDesc($id){
$db = new SQLiteDB(dbfile);
$sql = "SELECT description from ay_content where id = ".$id;
$ret = query($db,$sql);
$row = $ret->fetchArray(SQLITE3_ASSOC);
return ($row['description']);
}
?>百度语音合成路口文件:
<?php
include("../include/SQLite.php");
require_once 'AipSpeech.php';
// 你的 APPID AK SK
const APP_ID = '百度id';
const API_KEY = '百度key';
const SECRET_KEY = 'baidu_SECRET';
$itemid=$_GET['id'];
//判断文件是否存在,存在的话就不继续了
$filename = $itemid.'.mp3';
//创建文件夹
$dir = "../static/audio";
if(creatDir($dir)){
$filename =realpath($dir) .'\\'. $filename;
//echo $filename;
}
if(!file_exists($filename)){
echo "开始生成音频";
$title = getDesc($itemid);
if($title == "" || $title == ' '){
exit();
}else{
echo $title;
}
$slogn="更多内容请阅读xxx";
$content=$title.$slogn;
$client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
$result = $client->synthesis($content, 'zh', 6, array(
'vol' => 5,
));
// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
file_put_contents($filename, $result);
//下面两行配合使用可以输出音频文件
//header('Content-Type: audio/mpeg');
// echo $result;
}
}
function creatDir($dir){
//echo $dir;
//要创建的多级目录
$path=$dir;
//判断目录存在否,存在给出提示,不存在则创建目录
if (is_dir($path)){
return true;
}else{
//第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码
$res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);
if ($res){
return true;
}else{
return false;
}
}
}
?>其中require_once 'AipSpeech.php';是引用百度的文件

音频文件生成目录:

前端新闻模板页:news.html
<!-- 在head中引用jquery-->
<script src="/skin/js/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<!-- 这里可以放在head中也可以放到bady中-->
<script>
var id = '{content:id}';
$(function(){
$.ajax({
type: "GET",
url: "/speek/index.php",
data: {id:id},
dataType: "text",
success: function(data){
console.log(data);
var audio = '<audio controls="controls">'+
'<source src="/static/audio/{content:id}.mp3" type="audio/mp3" />'+
'Your browser does not support this audio format.</audio>';
$('.audio_c').append(audio);
//audio_c是需要插入的音频位置的类名
}
});
});
</script>
<!-- 把下面的语句放到需要插入的文件位置上-->
<p class="audio_c" style="text-align: center;"></p>
================如果需要看详细代码,在下面==============
边栏推荐
- 丹麦技术大学首创将量子计算应用于能源系统潮流建模
- 论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
- 汇编语言(4)函数传参
- MySQL gets the primary key and table structure of the table
- 改造一下 BeanUtils,优雅的实现 List 数据拷贝
- Yasea APK Download Image
- Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
- Install mysql5.6 under linux64bit - the root password cannot be modified
- Ecological escort cloud service providers wave "Intel flag"
猜你喜欢
随机推荐
卷积与转置卷积
Golang example renewal lock: redis+channel+sync Mutex
腾讯完成全面上云 打造国内最大云原生实践
bindservice方法实现音乐播放暂停
Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
天书夜读笔记——内存分页机制
C语言边界计算和不对称边界
这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
Linux64Bit下安装MySQL5.6-不能修改root密码
Scala trait exercise
Distinguish between i++ and ++i seconds
Properties of DOM
Mysql database Chapter 1 Summary
LLVM TargetPassConfig
Welcome to the new world of Lenovo smart screen
15.线程同步的几种方法
欢迎来到联想智能大屏的新世界
重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
数组中关于sizeof()和strlen
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
https://download.csdn.net/download/u014401637/85355037








