Skip to content

Instantly share code, notes, and snippets.

@abarth500
Created June 13, 2012 07:32
Show Gist options
  • Save abarth500/2922538 to your computer and use it in GitHub Desktop.
Save abarth500/2922538 to your computer and use it in GitHub Desktop.
Google API (読み上げ)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>TM Labo: Google API - 読み上げ機能(非公式API)</title>
<style>
/* 等幅にしておく */
* {
font-family: monospace;
}
</style>
<script type="text/javascript">
// qの値に応じてmp3を返すURL
var URL = "http://translate.google.com/translate_tts?q=";
// var URL = "http://translate.google.com/translate_tts?q=i+love+programming";
var readAloud = function(element){
// 入力した文字の空白や区切りを'+'に置換
var query = element.value.replace(/\s/g, '+');
// HTMLAudioElementを生成
var audio = new Audio(URL+query);
// 再生
// onloadだとうまくいかなかった。Chromeだと別スレッドで読み込み終了次第再生する模様
audio.play();
}
</script>
</head>
<body>
<h1>Google API - 読み上げ機能(非公式API)</h1>
<p>
Googleの公式翻訳サイトで読み上げ機能があったので、<br />
きっとAPIがあるとおもい探してみたところ、非公式ながら発見!!<br />
早速つかってみました。<br />
※Chromeでは問題なく動きますが、Firefoxだと動かなかったです。IEは調べるまでもないw
</p>
<form name="fm" onsubmit="return false;">
<input type="text" name="q" value="Hello, world!" />
<input type="submit" onclick="readAloud(fm.q);" value="再生" />
</form>
<hr />
<a href="http://tm-labo.com/">Home</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment