当前位置:网站首页>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 ==============
边栏推荐
- Reading notes at night -- deep into virtual function
- Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
- 百度语音合成语音文件并在网站中展示
- 粉丝福利,JVM 手册(包含 PDF)
- 在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
- How to store dataframe data in pandas into MySQL
- C语言边界计算和不对称边界
- CCNP的BGP部分笔记
- Texture enhancement
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
猜你喜欢

Boutique enterprise class powerbi application pipeline deployment

JVM指令

void* 指针

百度语音合成语音文件并在网站中展示

带马尔科夫切换的正向随机微分方程数值格式模拟

Deploy a production cluster using Loki microservice pattern

Use redis' sorted set to make weekly hot Reviews

Tianshu night reading notes -- memory paging mechanism

Abnova a4gnt polyclonal antibody

Bi-sql delete
随机推荐
Hands on data analysis data modeling and model evaluation
Ecological escort cloud service providers wave "Intel flag"
腾讯云WeCity丨你好 2022!
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
IPC机制
期望与方差
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
Deploy a production cluster using Loki microservice pattern
This national day! Tencent cloud wecity will accompany you to travel and light up the city landmark
汇编语言(4)函数传参
"One good programmer is worth five ordinary programmers!"
天书夜读笔记——内存分页机制
修身励学篇
脱氧核糖核酸酶I中英文说明书
Abnova丨BSG 单克隆抗体中英文说明
Bi-sql between
MySQL gets the primary key and table structure of the table
1. package your own scaffold 2 Create code module
Bi SQL constraints
MySQL multi condition matching fuzzy query
https://download.csdn.net/download/u014401637/85355037