Dynamically updated mp3 url (overwrites repeatedly)

quickStart() function will constantly fetch my synthesized-speech at the server-side. Synthesize-speech will always overwrite to ‘output.mp3’. Hence, output.mp3 is dynamically updated.

My issue is, when I synthesize the word “hello”, the audioPlayer will play the output.mp3 for “hello” well. However, when I go to the next word “there”, the audioPlayer will still play the output.mp3 for “hello”, and not “there”. Similarly, when I go to my 3rd word, i.e. “my”, it still plays “hello”.

Checking my github directory, the “output.mp3” plays “hello”, “there” and “my” when I checked each time I execute quickStart(). Does it mean the url is not updating the output.mp3 after its updated.

Oddly, this is only within android web browsers and desktop web browsers. iOS web browsers work perfectly fine. Everytime I run quickStart(word), the audioPlayer will play the right word each time.

This is my code:

// Define an endpoint to handle requests for text-to-speech synthesis
app.get('/synthesize-speech', async (req, res) => {
    try {
        // Get the word from the query parameters
        const text = req.query.word;

        if (!text){
            return res.status(400).json({ error: 'No word passed over' });
        }

        // Construct the request
        const request = {
            input: { text: text },
            voice: { languageCode: 'en-IN', name: 'en-IN-Neural2-D', ssmlGender: 'FEMALE' },
            audioConfig: { audioEncoding: 'MP3', speakingRate: 1, pitch: -2 },
        };

        // Perform the TTS request
        const [response] = await client.synthesizeSpeech(request);

        // Write audio content to local file
        const writeFile = util.promisify(fs.writeFile);
        const outputPath = path.join(__dirname, 'public', 'output.mp3');
        await writeFile(outputPath, response.audioContent, 'binary');

        res.json({ success: true })
    }    
    catch(error){
        console.error('Error:', error);
        res.status(500).json({ error: 'Internal server error' });
    }
});

// Function to synthesize speech for a word and play it
        async function quickStart(word) {

            // Placeholder function for text-to-speech logic
            console.log(`Processing word: ${word} and ${currentIndex}`);

            // Make a GET request to the server to generate and serve audio files
            const response = await fetch(`/synthesize-speech?word=${word}`);
            
            if (!response.ok) {
                throw new Error('Failed to fetch audio file');
            }

            const audioBlob = await response.blob();
            const audioUrl = window.URL.createObjectURL(audioBlob);

            // Set the source of the audio player to the dynamically generated URL
            audioPlayer.src = audioUrl;
            
            // Create a new audio element
            // const audioElement = new Audio(audioUrl);

            // Add event listener to start audio only if it's not already added
            if (!$("#startAudio").data("eventAdded")) {
                $("#startAudio").on("click", function(e) {
                    if (!audio) {
                        audio = new Audio('/output.mp3');
                    }
                });
                $("#startAudio").data("eventAdded", true);
            }
            
            // Add event listener to play audio when it's ready
            audioPlayer.onloadedmetadata = function() {
                audioPlayer.play();
            };
        }
        <audio controls id="audioPlayer">
            Your browser does not support the audio element.
        </audio>

New contributor

P R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật