Skip to main content

Convert Audio

The convertAudio method in this library is used to convert an audio file to a different format and bit rate.

It provides options to specify the input audio file, the desired audio bit rate, and the output audio format.

Parameters

  • options (object): An object containing the following properties,See Options:

    • audioFileUrl (string): The URL or path of the input audio file.

    • audioBitRate (optional, enum): The desired audio bit rate. Default to BITRATE_128.

    • outputFormat (optional, enum): The desired output audio format. Default to AUDIO_MP3.

Return Value

This method returns a Promise that resolves to a string representing the path or URL of the converted audio file.

ConvertAudioOptions Type

This type represents the options that can be passed to the convertAudio function.

  • audioFileUrl (string): The URL or path of the input audio file.
  • audioBitRate (optional, enum): The desired audio bit rate, which can be one of the values from the AUDIO_BITRATES enum.
  • outputFormat (optional, enum): The desired output audio format, which can be one of the values from the
    AUDIO_OUTPUT_FORMAT enum.

Enums

AUDIO_BITRATES Enum

  • BITRATE_96: Represents a bit rate of 96 kbps.
  • BITRATE_112: Represents a bit rate of 112 kbps.
  • BITRATE_128: Represents a bit rate of 128 kbps.
  • BITRATE_160: Represents a bit rate of 160 kbps.
  • BITRATE_192: Represents a bit rate of 192 kbps.
  • BITRATE_256: Represents a bit rate of 256 kbps.
  • BITRATE_320: Represents a bit rate of 320 kbps.

AUDIO_OUTPUT_FORMAT Enum

  • AUDIO_AAC: Represents the AAC audio format.
  • AUDIO_MP3: Represents the MP3 audio format.

Example Usage

import * as React from 'react';

import {
Button,
Dimensions,
StyleSheet,
Text,
ToastAndroid,
View,
} from 'react-native';
import {
type ConvertAudioOptions,
convertAudio,
AUDIO_OUTPUT_FORMAT,
} from 'rnvideoeditor';

const ConvertAudio = () => {
const convertAsync = async () => {
try {
const options: ConvertAudioOptions = {
audioFileUrl:
'file:///storage/emulated/0/Android/data/com.rnvideoeditorexample/cache/RNVideoEditor_audio_converted.aac',
outputFormat: AUDIO_OUTPUT_FORMAT.AUDIO_MP3,
};
const startProcessing = performance.now();
const uri = await convertAudio(options);
const endProcessing = performance.now();
ToastAndroid.show(
`Audio Converted in ${millisecondsToSeconds(
endProcessing - startProcessing
).toFixed(3)} s`,
ToastAndroid.SHORT
);
console.log(uri);
} catch (error) {
console.log(error);
}
};
return (
<View style={[styles.box]}>
<Text>Convert Audio </Text>
<Button title="Convert Audio" onPress={convertAsync} />
</View>
);
};

const styles = StyleSheet.create({
box: {
width: Dimensions.get('window').width - 100,
height: 80,
marginVertical: 20,
},
});