{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["admonition","partial"]},"type":"markdown"},"seo":{"title":"Code Samples","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"code-samples","__idx":0},"children":["Code Samples"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Use the code samples below to quickly get started developing with the SDK."]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["These examples require the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/java"},"children":["Rev AI Java SDK"]},"."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"submit-a-local-file-for-transcription","__idx":1},"children":["Submit a local file for transcription"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example demonstrates how to submit a local audio file for transcription."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<FILEPATH>"]}," placeholder with the path to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"java","header":{"controls":{"copy":{}}},"source":"String accessToken = \"<REVAI_ACCESS_TOKEN>\";\nString localPathToFile = \"<FILEPATH>\";\n\n// initialize the client with your access token\nApiClient apiClient = new ApiClient(accessToken);\n\n// submit a local file\nRevAiJob revAiJob = apiClient.submitJobLocalFile(localPathToFile);\n\n// check job status\nRevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());\n\n// retrieve transcript\n// as plain text\nString transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());\n\n// or as an object\nRevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());\n","lang":"java"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"submit-a-remote-file-for-transcription","__idx":2},"children":["Submit a remote file for transcription"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example demonstrates how to submit a remote audio file for transcription."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<URL>"]}," placeholder with the public URL to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"java","header":{"controls":{"copy":{}}},"source":"String accessToken = \"<REVAI_ACCESS_TOKEN>\";\nString urlLinkToFile = \"<URL>\";\n\n// initialize the client with your access token\nApiClient apiClient = new ApiClient(accessToken);\n\n// submit a remote file\nRevAiJobOptions revAiJobOptions = new RevAiJobOptions();\nrevAiJobOptions.setSourceConfig(urlLinkToFile);\nRevAiJob revAiJob = apiClient.submitJobUrl(revAiJobOptions);\n\n// check job status\nRevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());\n\n// retrieve transcript\n// as plain text\nString transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());\n\n// or as an object\nRevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());\n","lang":"java"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"submit-an-audio-stream-for-transcription","__idx":3},"children":["Submit an audio stream for transcription"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example demonstrates how to submit an audio stream for transcription."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<FILEPATH>"]}," placeholder with the path to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"java","header":{"controls":{"copy":{}}},"source":"String accessToken = \"<REVAI_ACCESS_TOKEN>\";\nString filePath = \"<FILEPATH>\";\n\n// initialize the client with your access token\nApiClient apiClient = new ApiClient(accessToken);\n\n// submit from a stream \nFile file = new File(filePath);\nFileInputStream fileInputStream;\ntry {\n  fileInputStream = new FileInputStream(file);\n} catch (FileNotFoundException e) {\n  throw new RuntimeException(\"Could not find file [\" + file.getName() + \"]\");\n}\nRevAiJob revAiJob = apiClient.submitJobLocalFile(fileInputStream, String fileName, RevAiJobOptions options);\n\n// check job status\nRevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());\n\n// retrieve transcript\n// as plain text\nString transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());\n\n// or as an object\nRevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());\n","lang":"java"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"stream-a-local-file","__idx":4},"children":["Stream a local file"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example can be used to configure a streaming client, stream audio from a file, and obtain the transcript as the audio is processed."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<FILEPATH>"]}," placeholder with the path to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"java","header":{"controls":{"copy":{}}},"source":"import ai.rev.speechtotext.RevAiWebSocketListener;\nimport ai.rev.speechtotext.SessionConfig;\nimport ai.rev.speechtotext.models.streaming.StreamContentType;\nimport ai.rev.speechtotext.models.streaming.SessionConfig;\nimport ai.rev.speechtotext.models.streaming.ConnectedMessage;\nimport ai.rev.speechtotext.models.streaming.Hypothesis;\nimport okhttp3.Response;\nimport okio.ByteString;\n\nimport java.io.BufferedInputStream;\nimport java.io.DataInputStream;\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.nio.ByteBuffer;\nimport java.util.Arrays;\n\npublic class RevAiStreaming {\n\n  public void streamFromLocalFile() throws InterruptedException {\n    String accessToken = \"<REVAI_ACCESS_TOKEN>\";\n    String filePath = \"<FILEPATH>\";\n\n    // Configure the streaming content type\n    StreamContentType streamContentType = new StreamContentType();\n    streamContentType.setContentType(\"audio/x-raw\"); // audio content type\n    streamContentType.setLayout(\"interleaved\"); // layout\n    streamContentType.setFormat(\"S16LE\"); // format\n    streamContentType.setRate(16000); // sample rate\n    streamContentType.setChannels(1); // channels\n\n    // Setup the SessionConfig with any optional parameters\n    SessionConfig sessionConfig = new SessionConfig();\n    sessionConfig.setMetaData(\"Streaming from the Java SDK\");\n    sessionConfig.setFilterProfanity(true);\n\n    // Initialize your client with your access token\n    StreamingClient streamingClient = new StreamingClient(accessToken);\n\n    // Initialize your WebSocket listener\n    WebSocketListener webSocketListener = new WebSocketListener();\n\n    // Begin the streaming session\n    streamingClient.connect(webSocketListener, streamContentType, sessionConfig);\n\n    // Read file from disk\n    File file = new File(filePath);\n\n    // Convert file into byte array\n    byte[] fileByteArray = new byte[(int) file.length()];\n    try (final FileInputStream fileInputStream = new FileInputStream(file)) {\n      BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);\n      try (final DataInputStream dataInputStream = new DataInputStream(bufferedInputStream)) {\n        dataInputStream.readFully(fileByteArray, 0, fileByteArray.length);\n      } catch (IOException e) {\n        throw new RuntimeException(e.getMessage());\n      }\n    } catch (IOException e) {\n      throw new RuntimeException(e.getMessage());\n    }\n\n    // Set the number of bytes to send in each message\n    int chunk = 8000;\n\n    // Stream the file in the configured chunk size\n    for (int start = 0; start < fileByteArray.length; start += chunk) {\n      streamingClient.sendAudioData(\n          ByteString.of(\n              ByteBuffer.wrap(\n                  Arrays.copyOfRange(\n                      fileByteArray, start, Math.min(fileByteArray.length, start + chunk)))));\n    }\n\n    // Wait to make sure all responses are received\n    Thread.sleep(5000);\n\n    // Close the WebSocket\n    streamingClient.close();\n  }\n\n  // Your WebSocket listener for all streaming responses\n  private static class WebSocketListener implements RevAiWebSocketListener {\n\n    @Override\n    public void onConnected(ConnectedMessage message) {\n      System.out.println(message);\n    }\n\n    @Override\n    public void onHypothesis(Hypothesis hypothesis) {\n      System.out.println(hypothesis.toString());\n    }\n\n    @Override\n    public void onError(Throwable t, Response response) {\n      System.out.println(response);\n    }\n\n    @Override\n    public void onClose(int code, String reason) {\n      System.out.println(reason);\n    }\n\n    @Override\n    public void onOpen(Response response) {\n      System.out.println(response.toString());\n    }\n  }\n}\n","lang":"java"},"children":[]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Find ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/revdotcom/revai-java-sdk/tree/master/examples"},"children":["more examples on GitHub"]},"."]}]}]},"headings":[{"value":"Code Samples","id":"code-samples","depth":1},{"value":"Submit a local file for transcription","id":"submit-a-local-file-for-transcription","depth":2},{"value":"Submit a remote file for transcription","id":"submit-a-remote-file-for-transcription","depth":2},{"value":"Submit an audio stream for transcription","id":"submit-an-audio-stream-for-transcription","depth":2},{"value":"Stream a local file","id":"stream-a-local-file","depth":2}],"frontmatter":{"title":"Java SDK Code Samples","toc":{"enable":true},"seo":{"title":"Code Samples"}},"lastModified":"2026-02-24T14:47:49.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/sdk/java/code-samples","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}