This guide provides a step-by-step approach to using the PlayAI’s Text-to-Speech API to convert text into natural human-like sounding audio.

In this example, we’ll have Dialog 1.0 create a simple audio from the given input text.

Prerequisites

  • Access your credentials (API key and user ID)
  • Development environment for your chosen programming language

Steps

1

Set Up Environment Variables

Add your API key and user ID to your environment variables.

echo 'export PLAYAI_API_KEY="your_api_key_here"' >> ~/.zshrc
echo 'export PLAYAI_USER_ID="your_user_id_here"' >> ~/.zshrc
source ~/.zshrc
2

Configure API Access

Create a script with the following authentication setup for your chosen language:

import os

api_key = os.getenv("PLAYAI_API_KEY")
user_id = os.getenv("PLAYAI_USER_ID")

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json',
    'X-USER-ID': user_id
}
3

Prepare API Parameters

Define your API payload with these key parameters:

  • model: Use PlayDialog for multi-turn conversation generation
  • text: Your input text for speech generation
  • voice: URL path to the voice manifest
  • output_format: Choose wav or mp3
json_data = {
    'model': 'PlayDialog',
    'text': "All human wisdom is summed up in these two words: Wait and hope.",
    'voice': 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
    'outputFormat': 'wav'
}
4

Implement API Call

Create the complete API implementation in your chosen language:

response = requests.post('https://api.play.ai/api/v1/tts/stream',
                       headers=headers,
                       json=json_data)

if response.status_code == 200:
    with open('dialogue.wav', 'wb') as f:
        f.write(response.content)
    print("Audio file saved as dialogue.wav")
else:
    print(f"Request failed with status code {response.status_code}: {response.text}")
5

Run and Test

Follow these steps to run your code:

  1. Save your code as playdialog_tts.py
  2. Open terminal in your code directory
  3. Run: python3 playdialog_tts.py
  4. Check for the generated dialogue.wav file
6

Customize and Adapt

Modify your implementation by:

  • Updating the input text
  • Changing speaker details and voices
  • Adjusting output format as needed
  • Adding multiple speakers or complex dialogues

Complete Code

import os
import requests

api_key = os.getenv("PLAYAI_API_KEY")
user_id = os.getenv("PLAYAI_USER_ID")

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json',
    'X-USER-ID': user_id
}

json_data = {
    'model': 'PlayDialog',
    'text': "All human wisdom is summed up in these two words: Wait and hope.",
    'voice': 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
    'outputFormat': 'wav'
}

response = requests.post('https://api.play.ai/api/v1/tts/stream',
                       headers=headers,
                       json=json_data)

if response.status_code == 200:
    with open('dialogue.wav', 'wb') as f:
        f.write(response.content)
    print("Audio file saved as dialogue.wav")
else:
    print(f"Request failed with status code {response.status_code}: {response.text}")

Troubleshooting

If you encounter issues, check these common problems:

  • Authentication Issues:

    • Verify your API key and user ID
    • Confirm the AUTHORIZATION header includes “Bearer ” prefix
  • API Endpoint Errors:

    • Verify the correct PlayAI’s Dialog 1.0 API endpoint URL
    • Confirm the model name is PlayDialog
  • Language-Specific Issues:

    • JavaScript: Ensure node-fetch is installed for Node.js environments
    • Go: Check for proper error handling and response body closing
    • Dart: Verify the http package is added to your pubspec.yaml
    • Swift: Make sure you’re running on macOS for file system access