1) Install mpg321
Tapez dans un terminal :
sudo apt-get install mpg321
2) Create a PHP file
Call it for exemple "speak.php" and put the following line: (don't forget the opening tags and PHP code closures)
exec('mpg321 "http://translate.google.com/translate_tts?tl=fr&q=Bonjour"');
So when will call the PHP file, the command line will be executed, and launch the mpg321 player mp3 file returns Google translate!
3) Add the www-data user to the audio group
If we do not add the user "www-data" to the audio group, our PHP command will not work, and we can not hear the Raspberry talk!
So type in a terminal:
sudo adduser www-data audio
4) Test !
Place this PHP file in the "/ var / www" of your Raspberry.
Then, type in the address of your browser Raspberry, followed by the name of your PHP file,
for example: http: //IP_RASP/speak.php (of course you need to be on the same network)
Normally, if your Raspberry is connected to a speaker / headphone and internet, you should hear "Hello"!
5) Raspberry say what you want
Just replace "hello" with a variable in your php file
if(isset($_GET['text']))
{
exec('mpg321 "http://translate.google.com/translate_tts?tl=fr&q='.urlencode($_GET['text']).'"');
}
So you can simply call the "http: //IP_RASP/speak.php text =?" Followed by the text you want your say Raspberry (encoded to a URL of course), so that the Raspberry expressed ! To encode the text to a URL (if you want to send accented text with spaces or special characters, ... use the "urlencode ()" in php or "encodeURIComponent ()" JavaScript!
I hope you enjoyed this tutorial, do not hesitate to ask questions in the comments if you have one ;)
No comments:
Post a Comment