Play audio through the call stream
# support
d
Hey wacky question here, is there a way to play an audio file? I'm using vapi to do training and would love to play back a snippet of a user's recordings for them.
v
Hi there! 👋 I understand you want to play audio files through Vapi, specifically to play back snippets of user recordings during training sessions. While I couldn't find information about extracting specific snippets from recordings using Vapi's built-in tools, I can definitely help with accessing recordings and playing audio through Vapi's call stream! Here's how you can work with recordings and audio playback in Vapi: ### 1. Accessing the Full Recording First, you'll need to retrieve the recording. You can do this in two ways:
Copy code
bash
# Using the GET /call/:id endpoint
curl -X GET 'https://api.vapi.ai/call/{callId}' \
-H 'Authorization: Bearer YOUR_API_KEY'
Or through code:
Copy code
typescript
const downloadRecording = async () => {
  // Fetch call data to get artifact URLs
  const call = await vapiClient.calls.get(callId);

  if (call.artifact?.recordingUrl) {
    // Access the recording URL
    const recordingUrl = call.artifact.recordingUrl;
  }
};
According to the [documentation](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1342498699615539222), you can also configure your assistant's
serverMessages
to include
"end-of-call-report"
to receive the recording URL via webhooks. ### 2. Enabling Call Recording Make sure recording is enabled in your assistant's configuration:
Copy code
javascript
// Configure your assistant's artifactPlan with recordingEnabled: true
const assistantConfig = {
  artifactPlan: {
    recordingEnabled: true
  }
  // other configuration...
};
### 3. Playing Audio Through the Call Stream To play audio through a Vapi call, you can use websockets to stream the audio. Here's a simplified approach based on the [websocket streaming documentation](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1341810370700120064): ```javascript // Establish transport connection first const vapiTransport = new WebSocket(
wss://api.vapi.ai/call/${callId}/transport
);
await new Promise(resolve => vapiTransport.once('open', resolve)); // Then connect to listen endpoint const vapiListen = new WebSocket(
wss://api.vapi.ai/call/${callId}/listen
); // To send audio data to the call (your recording snippet) function sendAudioSnippet(audioData) { if (vapiTransport.readyState === WebSocket.OPEN) { vapiTransport.send(audioData); } } ``` You would need to process your recording externally to extract the specific snippet you want to play, then stream that snippet through the websocket connection. Is there anything specific about this process you'd like me to clarify further? Sources: - [Discord Thread on Call Recordings](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1342498699615539222) - [Discord Thread on Audio Streaming](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1341810370700120064) - [Discord Thread on Call Data Access](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1341440757189578782)
d
Is this true? When I try this I'm getting nothing // Connect to WebSocket const ws = new WebSocket(controlUrl); // Handle connection open const socketPromise = new Promise((resolve, reject) => { ws.on("open", async () => { try { console.log("WebSocket connection established"); // Send audio data through WebSocket ws.send(audioBuffer); // Close the connection ws.close(); resolve(); } catch (error) { reject(error); } });
k
Thank you for reaching out to us. We've received your message and will get back to you as soon as possible.
Hi, checking if this is resolved/solved for you?
d
No it's still pending
k
Uh, can you share more information about your usage? What exactly you want to try out? If possible, share an example so I can see what we can do best for you.