首页 > 解决方案 > Microsoft Translate Voice 请求时限制 200-400 字

问题描述

我有一个 Microsoft Translate Voice 的 URL,让语音看起来像:

<audio controls autoplay>
  <source src="https://www.bing.com/tspeak?&format=audio%2Fmp3&language=en&IG=D2CBB80AA6824D9A91B0A5D1074FC4A1&IID=translator.5034.2&text=I’m Often at the End of My Rope as a Mom of Two" type="audio/mpeg">
</audio>

问题是:text="any text here"Any text here限制在 200-400 字左右。我不知道这里的原因。在Bing Translate中,我可以插入完整的 5000 个单词并单击按钮音频来收听。

有什么方法可以解决这个问题吗?微软对这个 URL 有限制吗?

有什么方法可以像微软翻译主页那样插入5000字吗?

标签: javascript

解决方案


如果您在 Bing 的网站上打开开发者控制台并开始播放声音,您会看到它发送的第一个 mp3 请求只有几个单词,当它读完后,它会发送另一个,依此类推。

你也可以这样做:

// When the DOM (basically the HTML) is loaded
document.addEventListener('DOMContentLoaded', function(){
    // Define your DOM elements
    var getAudioBtn = document.getElementById('getAudioBtn'),
        langSelect = document.getElementById("langSelect"),
        langSource = document.getElementById("langSource"),
        audioEl = document.getElementById('audioEl');

    // Setup an event listener on the button
    getAudioBtn.addEventListener('click', getContentTranslate);
    // Setup an listener on the audio onended event
    audioEl.addEventListener('ended', readChunkQueue);
    
    var chunkQueue = [], // Queue of chunks of text to read
        wordsPerChunk = 80, // Words per chunk
        language = 'en'; // Default language

    function getContentTranslate() {
        // Store the language
        language = langSelect.value;
        
        // Empty the chunks array
        chunkQueue = [];
        // Split the text into words
        var words = langSource.value.split(/ /g),
            tmp = []; // Temporary array for creating a chunk
        
        while(words.length) {
            // If out temporary chunk is full, add it to the list
            if (tmp.length === wordsPerChunk) {
                chunkQueue.push(tmp.join(' '));
                tmp = [];
            }
            tmp.push(words.shift());
        }
        if (tmp.length) {
            chunkQueue.push(tmp.join(' '));
        }

        // Start reading these chunks
        readChunkQueue();
    }
    
    function readChunkQueue() {
      // If the list is empty, stop
      if (!chunkQueue.length) {
        return;
      }
      // Get the first chunk in the list
      var chunk = chunkQueue.shift(),
          url = 'https://www.bing.com/tspeak?&format=audio%2Fmp3'
              + '&language=' + encodeURIComponent(language)
              + '&IG=D2CBB80AA6824D9A91B0A5D1074FC4A1&IID=translator.5034.2'
              + '&text=' + encodeURIComponent(chunk);
      // Set the URL as source for the audio element
      audioEl.setAttribute('src', url);
    }
});
<select id="langSelect">
  <option value="en">English</option>
  <option value="vi">Vietnamese</option>
</select>
<br>
<textarea id="langSource" placeholder="Enter text or webpage URL here">Obama Inaugural Address. 20th January 2009. My fellow citizens: I stand here today humbled by the task before us, grateful for the trust you have bestowed, mindful of the sacrifices borne by our ancestors. I thank President Bush for his service to our nation, as well as the generosity and cooperation he has shown throughout this transition. Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because We the People have remained faithful to the ideals of our forbearers, and true to our founding documents. So it has been. So it must be with this generation of Americans. That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet. These are the indicators of crisis, subject to data and statistics. Less measurable but no less profound is a sapping of confidence across our land - a nagging fear that America's decline is inevitable, and that the next generation must lower its sights. Today I say to you that the challenges we face are real. They are serious and they are many. They will not be met easily or in a short span of time. But know this, America - they will be met. On this day, we gather because we have chosen hope over fear, unity of purpose over conflict and discord. On this day, we come to proclaim an end to the petty grievances and false promises, the recriminations and worn out dogmas, that for far too long have strangled our politics. We remain a young nation, but in the words of Scripture, the time has come to set aside childish things. The time has come to reaffirm our enduring spirit; to choose our better history; to carry forward that precious gift, that noble idea, passed on from generation to generation: the God-given promise that all are equal, all are free, and all deserve a chance to pursue their full measure of happiness. In reaffirming the greatness of our nation, we understand that greatness is never a given. It must be earned. Our journey has never been one of short-cuts or settling for less. It has not been the path for the faint-hearted - for those who prefer leisure over work, or seek only the pleasures of riches and fame. Rather, it has been the risk-takers, the doers, the makers of things - some celebrated but more often men and women obscure in their labor, who have carried us up the long, rugged path towards prosperity and freedom.</textarea>
<br>
<button id="getAudioBtn">GET AUDIO</button>
<br>
<audio id="audioEl" autoplay controls></audio>


推荐阅读