Use this file to discover all available pages before exploring further.
This guide provides a step-by-step approach to using the PlayAI Text-to-Speech API to convert text into natural human-like sounding audio using the Async (non-streaming) API Endpoint.In this example, we’ll have Dialog 1.0 create a simple audio from the given input text.
Create and submit your TTS job in your chosen language:
json_data = { 'model': 'PlayDialog', 'text': "This is the greatest moment to be alive", 'voice': 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json', 'outputFormat': 'mp3', 'speed': 1, 'language': 'english',}response = requests.post('https://api.play.ai/api/v1/tts', headers=headers, json=json_data)if response.status_code == 201: job_id = response.json().get('id') print(f"Job submitted successfully. Job ID: {job_id}")else: print(f"Job submission failed with status code {response.status_code}: {response.text}")
4
Poll Job Status
Monitor your job’s progress in your chosen language:
polling_url = f'https://api.play.ai/api/v1/tts/{job_id}'while True: response = requests.get(polling_url, headers=headers) status = response.json()['output']['status'] if status == 'COMPLETED': audio_url = response.json()['output']['url'] print(f"Job completed. Audio URL: {audio_url}") break elif status == 'IN_PROGRESS': print("Job is still in progress. Retrying in 5 seconds...") time.sleep(5) else: print(f"Job failed or encountered an unknown status: {status}") break
5
Download and Save Audio
Save the generated audio file in your chosen language:
audio_url = response.json()['output']['url']audio_response = requests.get(audio_url)if audio_response.status_code == 200: with open('output.mp3', 'wb') as f: f.write(audio_response.content) print("Audio file saved as output.mp3")else: print(f"Failed to download audio. Status code: {audio_response.status_code}")