Convert MP3 to M4A in Java

Cloudmersive
2 min readSep 25, 2020

For the best levels of clarity and sound integrity, M4A is the ideal format for those working in music and sound production. Unlike our previous post concerning file conversion to M4A, this article will look at how you can perform this action in Java. The following API will allow you to convert most popular audio formats to M4A instantly.

Our first step should be to add a Jitpack reference to the repository:

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

Then, we will need to add a reference 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, AudioConvertToM4a:

// 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.audioConvertToM4a(inputFile, fileUrl, bitRate);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AudioApi#audioConvertToM4a");
e.printStackTrace();
}

Now, you can easily integrate M4A conversion into any of your applications. To retrieve your free API Key, visit the Cloudmersive website. This will give you access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive

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