Java Code Samples
Use the Java code samples below to quickly get started developing with the Rev AI APIs.
Submit a local file for transcription
attention
This example uses the Rev AI Java SDK.
The following example demonstrates how to submit a local audio file for transcription.
To use this example, replace the <FILEPATH>
placeholder with the path to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN>
placeholder with your Rev AI account's access token.
String accessToken = "<REVAI_ACCESS_TOKEN>";
String localPathToFile = "<FILEPATH>";
// initialize the client with your access token
ApiClient apiClient = new ApiClient(accessToken);
// submit a local file
RevAiJob revAiJob = apiClient.submitJobLocalFile(localPathToFile);
// check job status
RevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());
// retrieve transcript
// as plain text
String transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());
// or as an object
RevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());
Submit a remote file for transcription
attention
This example uses the Rev AI Java SDK.
The following example demonstrates how to submit a remote audio file for transcription.
To use this example, replace the <URL>
placeholder with the public URL to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN>
placeholder with your Rev AI account's access token.
String accessToken = "<REVAI_ACCESS_TOKEN>";
String urlLinkToFile = "<URL>";
// initialize the client with your access token
ApiClient apiClient = new ApiClient(accessToken);
// submit a remote file
RevAiJobOptions revAiJobOptions = new RevAiJobOptions();
revAiJobOptions.setSourceConfig(urlLinkToFile);
RevAiJob revAiJob = apiClient.submitJobUrl(revAiJobOptions);
// check job status
RevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());
// retrieve transcript
// as plain text
String transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());
// or as an object
RevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());
Submit an audio stream for transcription
attention
This example uses the Rev AI Java SDK.
The following example demonstrates how to submit an audio stream for transcription.
To use this example, replace the <FILEPATH>
placeholder with the path to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN>
placeholder with your Rev AI account's access token.
String accessToken = "<REVAI_ACCESS_TOKEN>";
String filePath = "<FILEPATH>";
// initialize the client with your access token
ApiClient apiClient = new ApiClient(accessToken);
// submit from a stream
File file = new File(filePath);
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException("Could not find file [" + file.getName() + "]");
}
RevAiJob revAiJob = apiClient.submitJobLocalFile(fileInputStream, String fileName, RevAiJobOptions options);
// check job status
RevAiJob newlyRefreshedRevAiJob = apiClient.getJobDetails(revAiJob.getJobId());
// retrieve transcript
// as plain text
String transcriptText = apiClient.getTranscriptText(revAiJob.getJobId());
// or as an object
RevAiTranscript revAiTranscript = apiClient.getTranscriptObject(revAiJob.getJobId());
Stream a local file
attention
This example uses the Rev AI Java SDK.
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.
To use this example, replace the <FILEPATH>
placeholder with the path to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN>
placeholder with your Rev AI account's access token.
import ai.rev.speechtotext.RevAiWebSocketListener;
import ai.rev.speechtotext.SessionConfig;
import ai.rev.speechtotext.models.streaming.StreamContentType;
import ai.rev.speechtotext.models.streaming.SessionConfig;
import ai.rev.speechtotext.models.streaming.ConnectedMessage;
import ai.rev.speechtotext.models.streaming.Hypothesis;
import okhttp3.Response;
import okio.ByteString;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class RevAiStreaming {
public void streamFromLocalFile() throws InterruptedException {
String accessToken = "<REVAI_ACCESS_TOKEN>";
String filePath = "<FILEPATH>";
// Configure the streaming content type
StreamContentType streamContentType = new StreamContentType();
streamContentType.setContentType("audio/x-raw"); // audio content type
streamContentType.setLayout("interleaved"); // layout
streamContentType.setFormat("S16LE"); // format
streamContentType.setRate(16000); // sample rate
streamContentType.setChannels(1); // channels
// Setup the SessionConfig with any optional parameters
SessionConfig sessionConfig = new SessionConfig();
sessionConfig.setMetaData("Streaming from the Java SDK");
sessionConfig.setFilterProfanity(true);
// Initialize your client with your access token
StreamingClient streamingClient = new StreamingClient(accessToken);
// Initialize your WebSocket listener
WebSocketListener webSocketListener = new WebSocketListener();
// Begin the streaming session
streamingClient.connect(webSocketListener, streamContentType, sessionConfig);
// Read file from disk
File file = new File(filePath);
// Convert file into byte array
byte[] fileByteArray = new byte[(int) file.length()];
try (final FileInputStream fileInputStream = new FileInputStream(file)) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try (final DataInputStream dataInputStream = new DataInputStream(bufferedInputStream)) {
dataInputStream.readFully(fileByteArray, 0, fileByteArray.length);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
// Set the number of bytes to send in each message
int chunk = 8000;
// Stream the file in the configured chunk size
for (int start = 0; start < fileByteArray.length; start += chunk) {
streamingClient.sendAudioData(
ByteString.of(
ByteBuffer.wrap(
Arrays.copyOfRange(
fileByteArray, start, Math.min(fileByteArray.length, start + chunk)))));
}
// Wait to make sure all responses are received
Thread.sleep(5000);
// Close the WebSocket
streamingClient.close();
}
// Your WebSocket listener for all streaming responses
private static class WebSocketListener implements RevAiWebSocketListener {
@Override
public void onConnected(ConnectedMessage message) {
System.out.println(message);
}
@Override
public void onHypothesis(Hypothesis hypothesis) {
System.out.println(hypothesis.toString());
}
@Override
public void onError(Throwable t, Response response) {
System.out.println(response);
}
@Override
public void onClose(int code, String reason) {
System.out.println(reason);
}
@Override
public void onOpen(Response response) {
System.out.println(response.toString());
}
}
}