简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“长度”(…)

[英]Uncaught TypeError: Cannot read property 'length' of undefined(…)

I'm creating a speech to text convertor using webKitSpeechRecongizer, i followed well the tutorial about this API but I'm failing to get the returned texts and I get this error ("Uncaught TypeError: Cannot read property 'length' of undefined(…)").I don't have any idea about what this error is(just a student). 我正在使用webKitSpeechRecongizer创建到文本转换器的语音,我很好地遵循了有关此API的教程,但是我无法获取返回的文本,并且出现了此错误(“未捕获的TypeError:无法读取未定义的属性'length'(... )“)。我不知道这个错误是什么(只是一个学生)。 Any help will be appreciated.Below is the code for speech to text convertor. 任何帮助将不胜感激。下面是语音到文本转换器的代码。 Speech to text convertor 语音到文本转换器

<style type="text/css">
    body{
        font-family: Arial;
    }
    #result{
        height: 200px;
        border: 1px solid #ccc;
        padding: 10px;
        box-shadow: 0 0 10px 0 #bbb;
        margin-bottom: 30px;
        font-size: 14px;
        line-height: 25px;
    }   

    button{

        font-size: 20px;
        position: absolute;
        top: 240px;
        left: 50%;
    }
</style>

</head>
<body>
<h4 align="center">Speech to text convertor</h4>
<div id = "result"></div>
<button onclick="startConverting();"><i class = "fa fa-microphone">      </i></button>
<script type="text/javascript">

var r = document.getElementById('result');

function startConverting (){


    if('webkitSpeechRecognition' in window){
    var speechRecognizer = new webkitSpeechRecognition();   
    speechRecognizer.continuous = true;
    speechRecognizer.interimResults = true;
    speechRecognizer.lang = "en-GB";
    speechRecognizer.start();

    var finalTranscripts = '';

    speechRecognizer.onresult = function (event){
        var interimTranscripts = '';

        for(var i = event.resultIndex; i < event.result.length; i++){
            var transcript = event.results[i][0].transcript;
            transcript.replace("\n", "<br>");
            if (event.results[i].isFinal){
                finalTranscripts += transcript;
            }else{
                interimTranscripts += transcript;
            }
        }
        r.innerHTML = finalTranscripts + '<span style = "color:#999">' +  interimTranscripts + '</span>';
    };

    speechRecognizer.onerror = function (event){

    };
}else{
    r.innerHTML = 'Your browser is not supported.If Google chrome,please upgrade!';
}



}



</script>
</body>
</html>

Wov, I actually never heard of it but it seems I got it working now. 沃夫,我实际上从未听说过它,但是看来我现在可以使用它了。

var r = document.getElementById('result');
var btn = document.getElementById('btn');

btn.addEventListener('click', startConverting);

function startConverting() {
  if ('webkitSpeechRecognition' in window) {
    var speechRecognizer = new webkitSpeechRecognition();

    speechRecognizer.continuous = true;
    speechRecognizer.interimResults = true;
    speechRecognizer.lang = "en-GB";
    speechRecognizer.start();

    speechRecognizer.onresult = function(event) {
      if (event.results.length) {
        r.innerHTML = event.results[0][0].transcript;
      }
    };

    speechRecognizer.onerror = function(event) {

    };
  } else {
    r.innerHTML = 'Your browser is not supported.If Google chrome,please upgrade!';
  }
}

Here is the fiddle: https://jsfiddle.net/mehmetb/afd1jn2L/ 这是小提琴: https : //jsfiddle.net/mehmetb/afd1jn2L/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined Uncaught TypeError:无法读取未定义的属性“ length”…? - Uncaught TypeError: Cannot read property 'length' of undefined…? 未捕获的类型错误:无法读取未定义的属性“长度” - Uncaught TypeError : Cannot read property 'length' of undefined Uncaught TypeError:无法读取未定义的属性“ length”? - Uncaught TypeError: Cannot read property 'length' of undefined? 无法读取未定义的Uncaught TypeError的属性“ length”: - Cannot read property 'length' of undefined Uncaught TypeError:? TypeError:a未定义+未捕获TypeError:无法读取未定义的属性“ length” - TypeError: a is undefined + Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM