Convert MP3 to WAV in Java
Retaining high levels of quality while working with large audio files necessitates the use of the WAV file format. This article will discuss how you can convert popular file formats like MP3 to WAV instantly using the following API.
First, we need to add a reference to the repository in pom.xml:
<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:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Now, we can call our function, AudioConvertToWav:
// 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.
BigDecimal sampleRate = new BigDecimal(); // BigDecimal | Optional; Specify the desired sample rate of the converted audio file in kHz. Value may be between 8 and 96. Standard for audio CDs is 44.1, while DVD audio standard is 48. By default, the optimal sample rate will be chosen automatically.
try {
byte[] result = apiInstance.audioConvertToWav(inputFile, fileUrl, sampleRate);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AudioApi#audioConvertToWav");
e.printStackTrace();
}
With this, you can provide your applications with the capability to convert any audio file to WAV. To retrieve your free API Key, visit the Cloudmersive website. This will give you access to 800 monthly calls across our library of APIs.