在Android Studio中设置环境变量GOOGLE_APPLICATION_CREDENTIALS

时间:2019-02-13 09:27:07

标签: android android-studio google-cloud-platform dialogflow

我想创建一个应用程序,该应用程序可以接收音频文件并了解讲话者的意图。我正在使用Dialogflow https://dialogflow.com/完成任务。

我在Eclipse中尝试了此Java代码,并且能够引起演讲者的意图。

user_id

然后在Eclipse的“运行配置”中设置环境变量GOOGLE_APPLICATION_CREDENTIALS。

但是,当我尝试在Android Studio中运行相同的代码时,出现以下错误。

public static QueryResult detectIntentAudio(
          String projectId,
          byte[] audioData,
          String sessionId,
          String languageCode)
          throws Exception {
        // Instantiates a client
        try (SessionsClient sessionsClient = SessionsClient.create()) {
          // Set the session name using the sessionId (UUID) and projectID (my-project-id)
          SessionName session = SessionName.of(projectId, sessionId);
          System.out.println("Session Path: " + session.toString());

          // Note: hard coding audioEncoding and sampleRateHertz for simplicity.
          // Audio encoding of the audio content sent in the query request.
          AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
          int sampleRateHertz = 44100;

          // Instructs the speech recognizer how to process the audio content.
          InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder()
              .setAudioEncoding(audioEncoding) // audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
              .setLanguageCode(languageCode) // languageCode = "en-US"
              .setSampleRateHertz(sampleRateHertz) // sampleRateHertz = 16000
              .build();

          // Build the query with the InputAudioConfig
          QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();

          // Read the bytes from the audio file
          byte[] inputAudio = audioData;



          //System.out.println("Path is:"+Paths.get(audioFilePath));
          // Build the DetectIntentRequest
          DetectIntentRequest request = DetectIntentRequest.newBuilder()
              .setSession(session.toString())
              .setQueryInput(queryInput)
              .setInputAudio(ByteString.copyFrom(inputAudio))
              .build();

          // Performs the detect intent request
          DetectIntentResponse response = sessionsClient.detectIntent(request);

          // Display the query result
          QueryResult queryResult = response.getQueryResult();
          System.out.println("====================");
          System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
          System.out.format("Detected Intent: %s (confidence: %f)\n",
              queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
          System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());

          return queryResult;
        }
}

我在〜/ .bashrc中设置了GOOGLE_APPLICATION_CREDENTIALS变量。 我尝试将变量放入studio.vmoptions中。 我无法解决问题。任何帮助将不胜感激。

0 个答案:

没有答案