Home>Article>Backend Development> How to implement intelligent voice broadcasting in PHP

How to implement intelligent voice broadcasting in PHP

coldplay.xixi
coldplay.xixi Original
2020-08-03 14:03:01 3443browse

PHP method to implement intelligent voice broadcast: first obtain time information and weather conditions; then synthesize speech; finally play the synthesized speech file, the code is [exec('sudo /usr/bin/play / tmp/audio.mp3')].

How to implement intelligent voice broadcasting in PHP

PHP method to implement intelligent voice broadcast:

Step one: Obtain time information

For example, the text content may be like this: "Master, good morning, today is 8:00 a.m. on December 18, 2017, Monday." Such content uses several of PHP's own You can do it with the time function, and then splice it into character text! The following are some simple example codes:

How to implement intelligent voice broadcasting in PHP

PHP implements intelligent voice broadcast of weather

Get time

Second cloth: Get Weather conditions

For example, the text content may be as follows: "Today the weather will be cloudy and sunny, the temperature is 5-15 degrees, the humidity is 80%, and the air pollution index is 69." It's a bit troublesome to find a reliable and free API. The weather information provided by many free APIs is relatively simple. It only has weather conditions and temperatures, but no future weather conditions. In the end, I just found a makeshift one. Does anyone have more information? Keep a finger on good APIs. The example code is as follows:

How to implement intelligent voice broadcasting in PHP

PHP implements intelligent voice broadcast of weather

Weather conditions

Step 3: Speech synthesis

This is the Baidu interface that is called. First of all, you have to register an account with Baidu and get the developer's key and secret. There will be some free calls, which is enough if you don't use it for commercial purposes. Got it! Then download the SDK provided by Baidu. The usage is very simple. The example code is as follows:

How to implement intelligent voice broadcasting in PHP

PHP implements intelligent voice weather broadcast

You can see the final return The content was saved to the file /tmp/audio.mp3 (the Ubuntu system is used here). There may be a write permission problem here. It is recommended that you add sudo when executing the script at the end.

Step 4: Play the synthesized voice file

We cannot use a music player to play it manually. In fact, Linux can also play music under the command line. You need to install a software, and I will give you the installation command directly under Ubuntu:

sudo apt-get install sox libsox-fmt-all

After the installation is completed, you can use the play command to play music, for example: play hello.mp3

So next we You can use PHP to execute the playback command. The example is as follows:

exec('sudo /usr/bin/play /tmp/audio.mp3');

Finally, run the script in Linux and let the script reside in the background. The example is as follows:

/usr/bin/php /var/www/demo/BaiduSound/index.php > /dev/null 2>&1 &

Related learning recommendations:php graphic tutorial

The above is the detailed content of How to implement intelligent voice broadcasting in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn