Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p id="unsupportMessage">ブラウザがサポートしていません。</p>
<button id="recognitionStart">音声認識開始</button>
<label><input type="checkbox" id="isContinue">連続認識</label>
<ul id="recognizedMessages" style="margin-top: 8px;">
              
            
!

CSS

              
                #unsupportMessage {
  color: red;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
li {
  border-bottom: 1px solid #eeeeee;
  padding: 4px;
}
              
            
!

JS

              
                // ブラウザが音声認識と音声読み上げに対応しているか確認
const unsupportMessage = $('#unsupportMessage')
if ('SpeechSynthesisUtterance' in window
    && 'webkitSpeechRecognition' in window) {
  unsupportMessage.hide()
} else {
  unsupportMessage.show()
}

// 要素の取得
const recognitionStart = $('#recognitionStart')
const isContinue = $('#isContinue')
const recognizedMessages = $('#recognizedMessages')

// 音声認識の初期化と設定
const recognition = new webkitSpeechRecognition();
recognition.lang = 'ja-JP'
recognition.continuous = true

// 音声読み上げの初期化と設定
const utterance = new SpeechSynthesisUtterance();
const voices = speechSynthesis.getVoices();
utterance.voice = voices[7]; // 日本語
utterance.volume = 1.0; // 音量
utterance.rate = 1.0; // 速度
utterance.pitch = 2.0; // 音程
utterance.lang = 'ja-JP'; // 言語

let hasResult = false; // recognition.onresultが呼ばれたか

// 音声認識開始
recognitionStart.on('click', () => {
  console.log('recognitionStart clicked')
  recognitionStart.prop('disabled', true)
  recognition.start();
  recognitionStart.text('お話しください')
});

recognition.onsoundstart = () => {
  console.log('recognition.onsoundstart')
}

recognition.onsoundend = () => {
  console.log('recognition.onsoundend')
  recognitionStart.text('音声認識開始')
}

// 音声認識結果取得時
recognition.onresult = (e) => {
  console.log('recognition.onresult')
  const msg = e.results[0][0].transcript;
  recognizedMessages.prepend('<li>' + msg + '</li>')
  utterance.text = msg
  
  speechSynthesis.speak(utterance); // 音声読み上げ開始
  
  recognition.stop(); // 音声認識終了
  hasResult = true;
}

recognition.onend = () => {
  console.log('recognition.onend')
  if (!hasResult) {
    recognitionStart.prop('disabled', false)
    recognitionStart.text('音声認識開始')
  }
  hasResult = false;
}

// 音声読み上げ終了時
utterance.onend = () => {
  console.log('utterance.onend')
  recognitionStart.prop('disabled', false)
  if (isContinue.prop('checked')) {
    recognitionStart.prop('disabled', true)
    recognition.start();
    recognitionStart.text('お話しください')
  }
}

              
            
!
999px

Console