2

我已使用我的 API 密钥成功地将 RAW 音频文件发送到 Google 的 Cloud Speech API。现在,我想使用 Chrome 的浏览器录制我的声音并发送,而不是“audio.raw”(就像在 Google.com 上一样)。我猜它应该是一个 HTML5 脚本。

这是我到目前为止所做的:

<?php
$data = json_encode(array(
    'config' => array(
        'encoding' => 'LINEAR16',
        'sample_rate' => 16000,
        'language_code' => 'en-US'
    ),
    'audio' => array(
        'content' => base64_encode(file_get_contents(dirname(__FILE__) . '/audio.raw'))
    )
));

$ch = curl_init('https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=XXX');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));

$result = json_decode(curl_exec($ch));

$text = (isset($result->results[0]->alternatives[0]->transcript) ? $result->results[0]->alternatives[0]->transcript : '');

echo $text;
?>
4

0 回答 0