辨識同步要求

將磁碟中的音訊檔案載入要求。

程式碼範例

C++

如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章

如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// Create a Speech client with the default configuration
auto client = speech::SpeechClient(speech::MakeSpeechConnection());
// Parse command line arguments.
auto const args = ParseArguments(argc, argv);
speech::v1::RecognizeRequest request;
*request.mutable_config() = args.config;
  // Load the audio file from disk into the request.
  auto content =
      std::string{std::istreambuf_iterator<char>(
                      std::ifstream(args.path, std::ios::binary).rdbuf()),
                  {}};
  request.mutable_audio()->mutable_content()->assign(std::move(content));
// Send audio content using Recognize().
auto response = client.Recognize(request);
if (!response) throw std::move(response).status();
// Dump the transcript of all the results.
for (auto const& result : response->results()) {
  for (auto const& alternative : result.alternatives()) {
    std::cout << alternative.confidence() << "\t" << alternative.transcript()
              << "\n";
  }
}

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器