Trim audio
The trimAudio
method lets you to trim an audio file, specifying a start time and an end time for the trimming operation.
You can also choose the output format of the trimmed audio file.
This function provides flexibility for precise audio editing.
Parameters
audioFileUri
(String): The uri of the audio file that you want to trim.startTimeInSecond
(Number): The start time (in seconds) from which the audio trimming begins.endTimeinSecond
(Number): The end time (in seconds) at which the audio trimming concludes.outputFormat
(Optional): The output format of the trimmed audio file.- Supported input and output formats include
.mp3
and.aac
. - By default, the output file will be type of
.aac
- Supported input and output formats include
Returns
A Promise that resolves with the following values:
onfulfilled
(String): Returns the output of trimmed audio file's uri.onrejected
(String): Returns a meaningful error message with context if an error occurs during the trimming process.
Usage
import React from 'react';
import {
View,
Button,
Alert,
StyleSheet,
Text,
Dimensions,
} from 'react-native';
import {
AudioTrimOutputFormat,
trimAudio,
type AudioTrimOptions,
} from 'rnvideoeditor';
const AudioTrimmer = () => {
const trim = async () => {
try {
const startProcessing = performance.now();
const trimOptions: AudioTrimOptions = {
audioFileUri:
'file:///storage/emulated/0/Android/data/com.rnvideoeditorexample/cache/sample3.aac',
endTimeinSecond: 25,
startTimeInSecond: 10,
outputFormart: AudioTrimOutputFormat.OUTPUT_MP3,
};
const trimmedAudioUri = await trimAudio(trimOptions);
console.log('Trimmed audio file: ', trimmedAudioUri);
const endProcessing = performance.now();
Alert.alert(`Trimmg done in ${Number(endProcessing - startProcessing)}`);
} catch (error) {
console.log(error);
}
};
return (
<View style={styles.box}>
<Button title="Trim Audio" onPress={trim} />
</View>
);
};
const styles = StyleSheet.create({
box: {
width: Dimensions.get('window').width - 100,
height: 80,
marginVertical: 20,
},
});