Top 10 Ways to Enhance Your Audio with Play.ht API

Jennie Lee
4 min readMar 31, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to Play.ht API

The play.ht API is a powerful tool that allows developers to enhance their audio content by incorporating AI voices into their applications. With the play.ht API, you can fetch and play mp3 files of AI voices, providing an immersive and engaging experience for your users.

AI voices have gained significant popularity in recent years due to their ability to generate high-quality speech that sounds natural and human-like. They provide an alternative to prerecorded audio, enabling developers to create dynamic and interactive applications that can deliver personalized content to their users.

In this article, we will focus on fetching and playing mp3 files from the play.ht website using the play.ht API. We will provide step-by-step instructions on setting up the play.ht API, understanding the JavaScript code snippet, implementing the Fetch API, and handling the asynchronous nature of the mp3 file availability.

Setting up the Play.ht API

Before we can start fetching and playing mp3 files from play.ht using the API, we need to obtain the necessary credentials. These credentials consist of a secret key (SECRETKEY) and a user ID (USERID), which can be obtained from the play.ht website.

To obtain the credentials, follow these steps:

  1. Log in to the play.ht website using your account credentials.
  2. Navigate to the API Access section in your account settings.
  3. Locate the SECRETKEY and USERID values provided on the API Access page.

Once you have obtained the credentials, you can replace the placeholders in the code snippets provided in this article with your actual values. These credentials will be used to authenticate your requests to the play.ht API.

Understanding the JavaScript Code Snippet

To fetch and play mp3 files from play.ht, we will use the provided JavaScript code snippet. Let’s take a high-level overview of the code and understand its purpose and functionality.

async function speak(text) {
const headers = {
'Authorization': 'Bearer SECRETKEY',
'Content-Type': 'application/json',
};

const body = JSON.stringify({
'text': text,
'voice': 'en-US',
'speed': '1',
'format': 'mp3',
});

const response = await fetch('https://play.ht/api/transcribe', {
method: 'POST',
headers: headers,
body: body
});

const responseData = await response.json();
const transcriptionId = responseData.transcriptionId;

let audioUrl = `https://media.play.ht/full_${transcriptionId}.mp3`;
while (true) {
try {
const audio = new Audio(audioUrl);
audio.play();
break;
} catch (error) {
console.log("Error occurred while fetching audio. Retrying...");
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}

The speak function takes a text parameter as input and fetches the corresponding mp3 file from the play.ht API using a POST request. The function first sets the necessary headers, including the Authorization header with the SECRETKEY value.

The request body includes the text, voice, speed, and format parameters. The text parameter represents the text that will be converted into speech. The voice parameter specifies the desired AI voice (in this case, en-US). The speed parameter controls the speed at which the audio will be spoken. The format parameter indicates the desired audio file format (mp3 in this case).

After making the POST request, the API will return a response that includes a transcription ID. This transcription ID is essential for constructing the mp3 URL.

The code then constructs the mp3 URL using the transcription ID and attempts to play the audio using the browser’s Audio object. If an error occurs during the audio fetching process, the code will log a message and retry after a 1-second delay. This while-loop will continue until the audio file becomes available and is successfully played.

Implementing the Fetch API

To make HTTP requests to the play.ht API and fetch the mp3 files, we will use the Fetch API. The Fetch API provides a modern and more straightforward way to make asynchronous requests compared to traditional methods like XMLHttpRequest.

To use the Fetch API, follow these steps:

  1. Inside your JavaScript code or browser’s developer console, define the speak function as described in the previous section.
  2. Call the speak function with the desired text parameter, for example: speak("Hello, world!")

The Fetch API will take care of making the POST request to the play.ht API. The function will await the response from the API and then construct the mp3 URL to play the audio.

Handling the Asynchronous Nature of Mp3 File Availability

One challenge we may encounter when fetching the mp3 file is that it may not be immediately available after making the API request. To handle this, we have implemented a while-loop in the provided code snippet.

Inside the while-loop, the code attempts to fetch the audio using the Audio object's play method. If an error occurs, indicating that the audio file is not yet available, the code logs a message and retries after a 1-second delay using the setTimeout function.

The while-loop will continue until the audio file is successfully fetched and played. This approach ensures that the audio is played only when it becomes available.

In conclusion, by leveraging the play.ht API, developers can enhance their audio content by incorporating AI voices into their applications. This article has provided a comprehensive guide on how to fetch and play mp3 files from the play.ht website using the play.ht API. It has covered setting up the API, understanding the JavaScript code snippet, implementing the Fetch API, and handling the asynchronous nature of the mp3 file availability. With this knowledge, you can now create engaging audio experiences for your users using the play.ht API.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet