当前位置:网站首页>Baidu voice synthesizes voice files and displays them on the website
Baidu voice synthesizes voice files and displays them on the website
2022-06-25 01:24:00 【Busy PK ends】
Project background :
Baidu speech synthesis api
pbootcms(sqlite3 edition ),mysql Or other websites can modify the process appropriately
Database operation file SQLite.php
<?php
//sqlite route
define("dbfile","../data/106aa56d9c6bda96a8e0829ace2466a6.db");
class SQLiteDB extends SQLite3
{
function __construct($file)
{
$this->open($file);
}
}
// according to sql Query table
function query($db,$sql){
$ret = $db->query($sql);
return $ret;
}
function close($db){
$db->close();
}
// according to id Get data remarks
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']);
}
?>Baidu voice synthesis intersection file :
<?php
include("../include/SQLite.php");
require_once 'AipSpeech.php';
// Yours APPID AK SK
const APP_ID = ' Baidu id';
const API_KEY = ' Baidu key';
const SECRET_KEY = 'baidu_SECRET';
$itemid=$_GET['id'];
// Judge whether the file exists , If it exists, it will not continue
$filename = $itemid.'.mp3';
// Create folder
$dir = "../static/audio";
if(creatDir($dir)){
$filename =realpath($dir) .'\\'. $filename;
//echo $filename;
}
if(!file_exists($filename)){
echo " Start generating audio ";
$title = getDesc($itemid);
if($title == "" || $title == ' '){
exit();
}else{
echo $title;
}
$slogn=" Read more xxx";
$content=$title.$slogn;
$client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
$result = $client->synthesis($content, 'zh', 6, array(
'vol' => 5,
));
// Recognize the correct return speech binary Error returns json Refer to the following error code
if(!is_array($result)){
file_put_contents($filename, $result);
// The following two lines can be used together to output audio files
//header('Content-Type: audio/mpeg');
// echo $result;
}
}
function creatDir($dir){
//echo $dir;
// Multi level directory to create
$path=$dir;
// Determine whether the directory exists , Prompt for presence , If not, create a directory
if (is_dir($path)){
return true;
}else{
// The third parameter is “true” Indicates that you can create multi-level directories ,iconv Prevent Chinese directory from being garbled
$res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);
if ($res){
return true;
}else{
return false;
}
}
}
?>among require_once 'AipSpeech.php'; It refers to Baidu's documents

Audio file generation Directory :

Front end news template page :news.html
<!-- stay head I quote jquery-->
<script src="/skin/js/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<!-- You can put it here head You can also put it in the middle bady in -->
<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 Is the class name of the audio location to be inserted
}
});
});
</script>
<!-- Put the following statement in the location of the file to be inserted -->
<p class="audio_c" style="text-align: center;"></p>
================ If you need to see the detailed code , Hereunder ==============
边栏推荐
- Introduction to bi-sql wildcards
- How to store dataframe data in pandas into MySQL
- SQL aggregate function handling null [easy to understand]
- Welcome to the new world of Lenovo smart screen
- 结合实操带你吃透Redis持久化
- Unity C# 网络学习(六)——FTP(一)
- 脱氧核糖核酸酶I中英文说明书
- Using macro code to generate handwriting automatically in word or WPS
- Bi-sql Union
- 全排列II[存在相同元素去重 + 标准回溯]
猜你喜欢

1. 封装自己的脚手架 2.创建代码模块

Introduction to bi-sql wildcards

Bi-sql index

Bi-sql - join

4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?

Cobalt strike installation tutorial

AUTOCAD——两种延伸方式
![uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法](/img/8b/0e982711c225ec8b0a2b90819d8a11.png)
uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法

Huawei laptop, which grew against the trend in Q1, is leading PC into the era of "smart office"

Bi SQL alias
随机推荐
Abnova a4gnt polyclonal antibody
sql 聚合函数有哪些
Redis and jedis
Abnova丨5-甲基胞嘧啶多克隆抗体中英文说明
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
期望与方差
Bi-sql delete
结合实操带你吃透Redis持久化
C语言边界计算和不对称边界
php easywechat 和 小程序 实现 长久订阅消息推送
中金证券靠谱吗?开证券账户安全吗?
PHP easywechat and applet realize long-term subscription message push
Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
PMP考试“临门一脚”如何踢得漂亮?
这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
Basic knowledge of assembly language (2) -debug
SQL aggregate function handling null [easy to understand]
Redis basic commands and types
天书夜读笔记——8.4 diskperf反汇编
高考之后,必然会出现以下四种情况:
https://download.csdn.net/download/u014401637/85355037