Convert WAV to MP3 in Java

Cloudmersive
2 min readSep 25, 2020

--

We have previously posted a tutorial on how to convert audio files to MP3 using Node.JS, and while our API remains the same, we can repackage it for use in other languages like Java. With this, you can instantly perform popular file conversions such as WAV, AAC, or M4A to MP3.

To integrate this API, our first step will be to add a reference to Jitpack to our repositories. This is shown below:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then, we can add a reference for our API client to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

After this, we can call our function, AudioConvertToMp3:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AudioApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
AudioApi apiInstance = new AudioApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fileUrl = "fileUrl_example"; // String | Optional; URL of an audio file being used for conversion. Use this option for files larger than 2GB.
Integer bitRate = 56; // Integer | Optional; Specify the desired bitrate of the converted audio file in kilobytes per second (kB/s). Value may be between 48 and 1,411. By default, the optimal bitrate will be chosen automatically.
try {
byte[] result = apiInstance.audioConvertToMp3(inputFile, fileUrl, bitRate);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AudioApi#audioConvertToMp3");
e.printStackTrace();
}

Now, you can easily add MP3 conversion capabilities to your application in Java! To retrieve your free API Key, you can go to the Cloudmersive website where you will gain access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet