当前位置:网站首页>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 ==============

be based on pbootcms+ Baidu voice synthesis automatically generates voice files and displays them on the news page -PHP Document resources -CSDN library https://download.csdn.net/download/u014401637/85355037

原网站

版权声明
本文为[Busy PK ends]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242055036658.html