wix 在 velo 中将二进制字符串转换为 mp3 文件的问题

问题描述 投票:0回答:0

我正在使用 Velo 进行一个项目,其中一个方面需要文本到语音,我正在使用 ElevenLabs api。 api 返回二进制代码,然后我需要将其放入 blob 中,以便将其附加到音频元素。但是,在尝试控制台记录 blob 时,我发现它一直给我空对象。

console.log(binarystring);
console.log(binarystring.length);
似乎显示了正确的内容,所以我不确定为什么我的其余代码不起作用。

import { fetch } from 'wix-fetch';

export function text_click(event) {
    const voiceId = '21m00Tcm4TlvDq8ikWAM';
    const apiKey = api; 
    const data = {
        text: 'Text',
        voice_settings: {
            stability: 0.1,
            similarity_boost: 0.8
        }
    };

    fetch(`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}/stream`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'audio/mpeg',
                'xi-api-key': apiKey
            },
            body: JSON.stringify(data) // Convert data object to JSON string and include it in the request body
        })
        .then(response => response.text())
        .then(response => {
            let binarystring = response;
            console.log(binarystring);
            console.log(binarystring.length); 

            const bytes = new Uint8Array(response.length);
            for (let i = 0; i < response.length; i++) {
                bytes[i] = response.charCodeAt(i);
            }

            console.log(bytes.buffer);

            let mp3Blob = new Blob([bytes.buffer], { type: 'audio/mpeg' });
            console.log(mp3Blob);
            $w('#audioPlayer1').src = URL.createObjectURL(mp3Blob); // Set the URL of the audio player to the URL of the blob
        })
        .catch(error => console.log(error));

}
javascript text-to-speech velo
© www.soinside.com 2019 - 2024. All rights reserved.