Opus4j
A Java wrapper for the Opus Codec
Install / Use
/learn @henkelmax/Opus4jREADME
Opus4J
A Java wrapper for the Opus Codec written in C using JNI.
Java 8+ is required to use this library.
Supported Platforms
Windows x86_64Windows aarch64macOS x86_64macOS aarch64Linux x86_64Linux aarch64
Usage
Maven
<dependency>
<groupId>de.maxhenkel.opus4j</groupId>
<artifactId>opus4j</artifactId>
<version>2.1.0</version>
</dependency>
<repositories>
<repository>
<id>henkelmax.public</id>
<url>https://maven.maxhenkel.de/repository/public</url>
</repository>
</repositories>
Gradle
dependencies {
implementation 'de.maxhenkel.opus4j:opus4j:2.1.0'
}
repositories {
maven {
name = "henkelmax.public"
url = 'https://maven.maxhenkel.de/repository/public'
}
}
Example Code
Encoding
short[] rawAudio = ...;
// Creates a new encoder instance with 48kHz mono VOIP
OpusEncoder encoder = new OpusEncoder(48000, 1, OpusEncoder.Application.VOIP);
// Sets the max payload size to 1500 bytes
encoder.setMaxPayloadSize(1500);
// Sets the max packet loss percentage to 1% for in-band FEC
encoder.setMaxPacketLossPercentage(0.01F);
// Encodes the raw audio
byte[] encoded = encoder.encode(rawAudio);
// Resets the encoder state
encoder.resetState();
...
// Closes the encoder - Not calling this will cause a memory leak!
encoder.close();
Decoding
byte[] encodedAudio = ...;
// Creates a new decoder instance with 48kHz mono
OpusDecoder decoder = new OpusDecoder(48000, 1);
// Sets the frame size to 960 samples
// If this is not set properly, decoded PLC/FEC frames will have the wrong size
decoder.setFrameSize(960);
// Decodes the encoded audio
short[] decoded = decoder.decode(encodedAudio);
// Decode a missing packet with PLC (Packet Loss Concealment)
decoded = decoder.decode(null);
// Decode a missing packet and the current packet with FEC (Forward Error Correction)
short[][] decodedFec = decoder.decode(encodedAudio, 2);
// Resets the decoder state
decoder.resetState();
...
// Closes the decoder - Not calling this will cause a memory leak!
decoder.close();
Building from Source
Prerequisites
Building
./gradlew build
Credits
Related Skills
node-connect
352.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
