首先:HTML如下:
<textarea id="speech" ng-model="speech.text" class="gwyj-textarea" maxlength="250" placeholder="請詳細描述你的崗位意見"></textarea>
上截圖:
再來:js中(我用的angularJS)
$scope.speech = {}; $scope.speech.text = ""; var text = ""; $scope.startRecognize = function() { if(plus.os.name == 'Android' && navigator.userAgent.indexOf('StreamApp') > 0) { plus.nativeUI.toast('當前環境暫不支持語音識別插件'); return; } var options = {}; options.engine = 'iFly'; plus.speech.startRecognize(options, function(s) { text = $("#speech").val() + s;
// 控制250字之間··· if (text.length>250) { text = text.substr(0,250); } else{ $("#speech").val(text); } console.log($scope.speech.text); }, function(e) { // alert("語音識別失敗!"); plus.nativeUI.toast('語音識別失敗'); }); // setTimeout( stopRecognize, 10000 ); } function stopRecognize() { plus.speech.stopRecognize(); }
截圖如下: