【问题标题】:Azure Cognitive Services TTS: Resource not found [closed]Azure 认知服务 TTS:找不到资源 [关闭]
【发布时间】:2020-12-26 09:16:11
【问题描述】:

我正在按照https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech 的说明尝试文字转语音。获取令牌正在工作并返回一个授权令牌。访问时 https://eastus.tts.speech.microsoft.com/cognitiveservices/v1 我收到 404 错误“资源不可用”。有什么建议? 我尝试使用作为 Azure 函数应用程序的一部分编写的 node.js 代码,以及使用邮递员 REST API 客户端。我为标题授权、内容类型和输出格式标题以及 SSML 格式的正文提供了值。

任何建议都将不胜感激。

谢谢

【问题讨论】:

    标签: javascript text-to-speech azure-cognitive-services azure-function-app


    【解决方案1】:

    请使用以下link 在 azure 门户中创建语音服务。

    以下是使用 Node.js 的示例代码。我们能够成功执行,如下图所示。

    // To install dependencies, run: npm install
    const xmlbuilder = require('xmlbuilder');
    // request-promise has a dependency on request
    const rp = require('request-promise');
    const fs = require('fs');
    const readline = require('readline-sync');
    
    
    // Gets an access token.
    function getAccessToken(subscriptionKey) {
        let options = {
            method: 'POST',
            uri: 'https://eastus.api.cognitive.microsoft.com/sts/v1.0/issueToken',
            headers: {
                'Ocp-Apim-Subscription-Key': subscriptionKey
            }
        }
        return rp(options);
    }
    
    // Converts text to speech using the input from readline.
    function textToSpeech(accessToken, text) {
        // Create the SSML request.
        let xml_body = xmlbuilder.create('speak')
            .att('version', '1.0')
            .att('xml:lang', 'en-us')
            .ele('voice')
            .att('xml:lang', 'en-us')
            .att('name', 'en-US-Guy24kRUS') // Short name for 'Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)'
            .txt(text)
            .end();
        // Convert the XML into a string to send in the TTS request.
        let body = xml_body.toString();
    
        let options = {
            method: 'POST',
            baseUrl: 'https://eastus.tts.speech.microsoft.com/',
            url: 'cognitiveservices/v1',
            headers: {
                'Authorization': 'Bearer ' + accessToken,
                'cache-control': 'no-cache',
                'User-Agent': 'YOUR_RESOURCE_NAME',
                'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
                'Content-Type': 'application/ssml+xml'
            },
            body: body
        }
    
        let request = rp(options)
            .on('response', (response) => {
                if (response.statusCode === 200) {
                    request.pipe(fs.createWriteStream('TTSOutput.wav'));
                    console.log('\nYour file is ready.\n')
                }
            });
        return request;
    
    };
    
    // Use async and await to get the token before attempting
    // to convert text to speech.
    async function main() {
        // Reads subscription key from env variable.
        // You can replace this with a string containing your subscription key. If
        // you prefer not to read from an env variable.
        // e.g. const subscriptionKey = "your_key_here";
        const subscriptionKey = process.env.SPEECH_SERVICE_KEY;
        if (!subscriptionKey) {
            throw new Error('Environment variable for your subscription key is not set.')
        };
        // Prompts the user to input text.
        const text = readline.question('What would you like to convert to speech? ');
    
        try {
            const accessToken = await getAccessToken(subscriptionKey);
            await textToSpeech(accessToken, text);
        } catch (err) {
            console.log(`Something went wrong: ${err}`);
        }
    }
    
    // Run the application
    main()
    

    https://github.com/Azure-Samples/Cognitive-Speech-TTS/tree/master/Samples-Http/NodeJS

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 2019-11-17
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多