HTML5 Speech recognition — is there a way to set what the user is expected to say dynamically? (Using custom Grammars)

后端 未结 2 1144
时光取名叫无心
时光取名叫无心 2020-12-30 06:33

I am looking for a way to define what you expect the user to say in an tag with the HTML 5 speech attribute set.
I know that you

相关标签:
2条回答
  • 2020-12-30 06:40

    The grammar file could be dynamically generated using something like PHP, JSP, or your favorite web development language. The grammar file is fetched using HTTP so you could have something like this if you are using PHP:

    <input type="text" speech grammar="grammar.php?some_var=foo" />
    

    The PHP would dynamically create the grammar based on information passed in a query string or through stored session information and return it to the speech engine.

    0 讨论(0)
  • 2020-12-30 06:51

    I found a way to do it client-side, using a new html5 feature: blobs.

    window.URL = window.URL || window.webkitURL;
    
    var myGrammar = new Blob(["My custom grammar"], {
         type: 'text/xml Or whatever is the proper MIME type for grammars'});
    
    var grammarUrl = window.URL.createObjectURL(myGrammar); 
    
    myInput = document.getElementById("myInput");
    
    myInput.grammar = grammarUrl;
    

    This makes a url out of the grammar string, and then sets that url for our input element.

    This way there is no need to make a server request, thus making it faster and less load on the server.

    For more information on blobs, see this and this.

    0 讨论(0)
提交回复
热议问题