如何在Node的google-assistant库中用音频文件替代麦克风输入?
Got it, let's break down how to feed a pre-recorded audio file into the Google Assistant library instead of relying on microphone input. I’ve worked with this library before, so here’s a practical, step-by-step solution that replaces the mic logic from the sample you mentioned.
First: Ensure Your Audio Meets Google’s Requirements
Google Assistant only accepts audio in a specific format: 16-bit PCM, mono (single channel), 16kHz sample rate. If your audio file is in MP3, WAV (with wrong specs), or another format, you’ll need to convert it first. The easiest way is with ffmpeg:
ffmpeg -i your-input-file.mp3 -acodec pcm_s16le -ac 1 -ar 16000 converted-audio.raw
This command converts your input to the exact format the Assistant expects.
Step-by-Step Code Changes
The original mic-speaker sample uses the mic module to capture real-time audio. We’ll replace that with Node’s built-in fs and stream modules to read your pre-recorded file. Here’s the modified code:
const fs = require('fs'); const { pipeline } = require('stream'); const Assistant = require('google-assistant'); // Use the same config from the original sample (credentials, device/model IDs, etc.) const config = { auth: { keyFilePath: './path-to-your-service-account-key.json', savedTokensPath: './tokens.json', // will be created automatically }, conversation: { lang: 'en-US', // adjust to your preferred language }, }; const assistant = new Assistant(config); // Replace mic stream with your pre-converted audio file const audioStream = fs.createReadStream('./converted-audio.raw'); assistant.start((conversation) => { // Pipe the audio file stream into the Assistant's input pipeline(audioStream, conversation.input, (err) => { if (err) { console.error('Error sending audio to Assistant:', err); conversation.end(); return; } // Once the audio file is fully sent, tell the Assistant we're done with input conversation.end(); }); // Handle Assistant's audio response (same as original sample) conversation.on('audio-data', (data) => { // You can write this to a file or play it with a speaker module // fs.writeFileSync('assistant-response.raw', data, { flag: 'a' }); }); // Handle conversation end events conversation.on('end', (error, continueConversation) => { if (error) { console.log('Conversation ended with error:', error); } else if (continueConversation) { console.log('Assistant is waiting for more input (you could send another file here)'); } else { console.log('Conversation completed successfully'); } }); });
What Changed From the Original Sample?
- Removed all
micmodule dependencies and logic (the part around line 55 where the mic stream is started). - Replaced the mic stream with
fs.createReadStream()pointing to your converted audio file. - Used
pipeline()instead of directpipe()to handle stream errors and cleanup more reliably. - Added an explicit
conversation.end()after the audio stream finishes, so the Assistant knows to stop waiting for input.
Critical Notes to Avoid Headaches
- Always convert your audio first: If you skip this step, the Assistant won’t understand your audio, and you’ll get no response or errors.
- Use
pipelineinstead ofpipe: It’s a Node.js utility that handles backpressure, error cleanup, and stream termination properly—something directpipe()doesn’t do consistently. - Handle conversation continuation: If the Assistant asks a follow-up question (
continueConversationistrue), you can send another audio file the same way by creating a new read stream and piping it to a new conversation.
内容的提问来源于stack exchange,提问作者K20GH

