This bundle integrates AWS Polly to your project. It installs the AWS SDK for PHP for the client and the AsyncAws Bundle for credentials.
Features
- Client
- Speech synthesizer
Open a command console, enter your app directory and execute the following command to download the latest stable version of this bundle:
composer require ang3/aws-polly-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Make sure you have configured the bundle AsyncAws, especially for authentication.
Create the file config/packages/ang3_aws_polly.yaml
, and add the contents below:
# config/packages/ang3_aws_polly.yaml
ang3_aws_polly:
region: 'YOUR_REGION'
Make sure to replace YOUR_REGION
by your AWS settings.
Be aware some voices are available only on specific regions or engine. Please refer to the AWS documentation.
Public service ID: ang3.aws_polly.client
To use the Polly
client, get it by dependency injection:
namespace App\Service;
use Aws\Polly\PollyClient;
class MyService
{
public function __construct(private PollyClient $client)
{
}
}
Public service ID: ang3.aws_polly.speech_synthesizer
To synthesize a speech, use dependency injection:
namespace App\Service;
use Ang3\Bundle\AwsBundle\Service\SpeechSynthesizer;
class MyService
{
public function __construct(private SpeechSynthesizer $speechSynthesizer)
{
}
}
Then, synthesize a speech with your text (mp3):
use Ang3\Bundle\AwsPollyBundle\Enum\Voice;
/** @var \Ang3\Bundle\AwsBundle\Service\SpeechSynthesizer $speechSynthesizer */
$audioFileUrl = $speechSynthesizer->create('Hello world!', Voice::AMY);
The function returns a secured URL to the MP3 file.
That's it!