前提・実現したいこと
音声認識APIを叩いてテキストを表示させるプログラムですが以下のようなエラーがIOSで起動すると出てしまいます。
発生している問題・エラーメッセージ
deviceready has not fired after 5 seconds Channel not fired onFileSystemPathsReady Channel not fired onCordovaInfoReady Channel not fired onCordovaConnectionReady
該当のソースコード
html JavaScript
1<!DOCTYPE HTML> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> 6 <meta http-equiv="Content-Security-Policy" content="default-src * data:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'"> 7 <script src="components/loader.js"></script> 8 <script src="lib/onsenui/js/onsenui.min.js"></script> 9 10 <link rel="stylesheet" href="components/loader.css"> 11 <link rel="stylesheet" href="lib/onsenui/css/onsenui.css"> 12 <link rel="stylesheet" href="lib/onsenui/css/onsen-css-components.css"> 13 <link rel="stylesheet" href="css/style.css"> 14 15 <script> 16 // Google Cloud Speech APIで使うAPIキー 17 var key = "AIzaSyCpWZ-vhQ3L6a7xM70tO9pjpWcINdwHYKU"; 18 19 ons.ready(function() { 20 var app = { 21 // ファイルを読み込むためのオブジェクトです 22 reader: new FileReader(), 23 24 // キャプチャ成功時のコールバックです 25 success: function(files) { 26 var file = files[0]; 27 $("#audio").attr("src", file.fullPath); 28 29 // ファイルを読み込んだ時のコールバックを指定 30 app.reader.onloadend = app.load_file; 31 32 // Fileオブジェクトを作成します 33 audioFile = new window.File( 34 file.name, 35 file.localURL, 36 file.type, 37 file.lastModifiedDate, 38 file.size 39 ); 40 41 // ファイルを読み込みます(Base64で取得できます) 42 app.reader.readAsDataURL(audioFile); 43 }, 44 45 // ファイルを読み込むと呼ばれるコールバックです 46 load_file: function() { 47 // base64文字列を取得 48 var result = app.reader.result; 49 var encoding; 50 var sample_rate; 51 52 if(result.indexOf("data:audio/amr;") === 0) { 53 // 音声データがamrの場合 54 encoding = "AMR"; 55 sample_rate = 8000; 56 } else { 57 // 音声データがwavの場合 58 encoding = "LINEAR16"; 59 sample_rate = 44100; 60 } 61 62 // data:audio/wav;base64,aaa... といった形式で取得 63 // されるので、,以降だけにします 64 var ary = result.split(","); 65 66 // Google Cloud Speech APIに投げるデータフォーマットを作成 67 var json = { 68 "config": { 69 "encoding": encoding, 70 "sample_rate": sample_rate, 71 "language_code": "ja_JP" 72 }, 73 "audio": { 74 "content": ary[1] 75 } 76 }; 77 78 // 音声認識処理を実行します 79 app.voice_recognition(json); 80 }, 81 82 // Google Cloud Speech APIをコールします 83 voice_recognition: function(json) { 84 // AjaxでPOST処理 85 $.ajax({ 86 type: 'POST', 87 url: 'https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=' + key, 88 data: JSON.stringify(json), 89 dataType:'json', 90 contentType: 'application/json' 91 }).done(function(data) { 92 // 処理結果をテキストエリアに表示 93 $("#result").val(data.results[0].alternatives[0].transcript); 94 }).fail(function(error) { 95 alert("Speech API Error:" + JSON.stringify(error)); 96 }); 97 }, 98 99 // キャプチャ失敗時のコールバックです 100 error: function(error) { 101 alert("Capture Error:" + error); 102 } 103 }; 104 105 $("#capture").on("click", function() { 106 alert("開始"); 107 navigator.device.capture.captureAudio(app.success, app.error, {limit:15}); 108 }); 109 }); 110 </script> 111</head> 112<body> 113 <ons-page> 114 <ons-toolbar> 115 <div class="center">音声認識</div> 116 </ons-toolbar> 117 118 <section style="padding: 8px"> 119 <p></p><br><br> 120 <ons-button id="capture" modifier="large">音声キャプチャ</ons-button> 121 <p></p> 122 <p><audio id="audio" controls></audio></p> 123 <textarea id="result" class="textarea"></textarea> 124 </section> 125 </ons-page> 126</body> 127</html>
あなたの回答
tips
プレビュー