Updated 12 January 2024
In this blog, we will learn how to Implement Easy Audio Trimmer In Flutter and Trim audio files using easy_audio_trimmer and save the trimmed audio files to the device.
You may also check our flutter app development company page.
An audio trimmer is essentially an audio editor designed to cut, trim, or split audio files.
It allows you to remove unwanted parts from your audio clips and save the resulting part of the audio in various audio file formats such as MP3, WAV, AAC, FLAC, OGG, and WMA.
Audio trimmers serve a fundamental purpose: enabling users to cut and refine audio clips according to their preferences.
this functionality finds applications in various scenarios, including creating customised ringtones, editing, or simply extracting relevant portions from lengthy recordings.
First we need to create a new flutter project and add the following dependencies in the pubspec.yaml file.
1 2 3 4 5 |
dependencies: flutter: sdk: flutter easy_audio_trimmer: ^1.0.2+4 file_picker: ^6.1.1 |
Now, run the command “flutter pub get” to add the dependencies.
Add the following package to your class.
1 2 3 |
import 'package:easy_audio_trimmer/easy_audio_trimmer.dart'; import 'package:file_picker/file_picker.dart'; |
First, we will create a button to pick the audio file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Center( child: ElevatedButton( style:ElevatedButton.styleFrom(backgroundColor:Colors.blue.shade50), onPressed: () async { FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.audio, allowCompression: false, ); if (result != null) { File file = File(result.files.single.path!); Navigator.of(context).push( MaterialPageRoute(builder: (context) { return AudioTrimmerView(file); }), ); } }, child: const Text( "Select File", style: TextStyle(color: Colors.blue), )), ), |
1 2 3 4 5 6 7 8 9 10 |
void _loadAudio() async { setState(() { isLoading = true; }); await _trimmer.loadAudio(audioFile: widget.file); setState(() { isLoading = false; }); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
_saveAudio() { setState(() { _progressVisibility = true; }); _trimmer.saveTrimmedAudio( startValue: _startValue, endValue: _endValue, audioFileName: DateTime.now().millisecondsSinceEpoch.toString(), onSave: (outputPath) { setState(() { _progressVisibility = false; }); debugPrint('OUTPUT PATH: $outputPath'); }, ); } |
1 2 3 4 |
await _trimmer.audioPlaybackControl( startValue: _startValue, endValue: _endValue, ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
TrimViewer( trimmer: _trimmer, viewerHeight: 100, viewerWidth: MediaQuery.of(context).size.width, durationStyle: DurationStyle.FORMAT_MM_SS, backgroundColor:Theme.of(context).primaryColor, barColor: Colors.white, durationTextStyle: TextStyle( color: Theme.of(context).primaryColor), allowAudioSelection: true, editorProperties: TrimEditorProperties( circleSize: 10, borderPaintColor: Colors.pink, borderWidth: 4, borderRadius: 5, circlePaintColor: Colors.pink.shade800, ), areaProperties: TrimAreaProperties.edgeBlur(blurEdges:true), onChangeStart: (value) => _startValue = value, onChangeEnd: (value) => _endValue = value, onChangePlaybackState: (value) { if (mounted) { setState(() => _isPlaying = value); } }, ), |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
import 'dart:io'; import 'package:easy_audio_trimmer/easy_audio_trimmer.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; class AudioTrimmer extends StatefulWidget { const AudioTrimmer({super.key}); @override State<AudioTrimmer> createState() => _AudioTrimmerState(); } class _AudioTrimmerState extends State<AudioTrimmer> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text( "Audio Trimmer", style: TextStyle(color: Colors.white), ), backgroundColor: Colors.blue, ), body: Center( child: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: Colors.blue.shade50), onPressed: () async { FilePickerResult? result = awaiFilePicker.platform.pickFiles( type: FileType.audio, allowCompression: false, ); if (result != null) { File file = File(result.files.single.path!); Navigator.of(context).push( MaterialPageRoute(builder: (context) { return AudioTrimmerView(file); }), ); } }, child: const Text( "Select File", style: TextStyle(color: Colors.blue), )), ), ); } } class AudioTrimmerView extends StatefulWidget { final File file; const AudioTrimmerView(this.file, {Key? key}) : super(key: key); @override State<AudioTrimmerView> createState() => _AudioTrimmerViewState(); } class _AudioTrimmerViewState extends State<AudioTrimmerView> { final Trimmer _trimmer = Trimmer(); double _startValue = 0.0; double _endValue = 0.0; bool _isPlaying = false; bool _progressVisibility = false; bool isLoading = false; @override void initState() { super.initState(); _loadAudio(); } void _loadAudio() async { setState(() { isLoading = true; }); await _trimmer.loadAudio(audioFile: widget.file); setState(() { isLoading = false; }); } _saveAudio() { setState(() { _progressVisibility = true; }); _trimmer.saveTrimmedAudio( startValue: _startValue, endValue: _endValue, audioFileName: DateTime.now().millisecondsSinceEpoch.toString(), onSave: (outputPath) { setState(() { _progressVisibility = false; }); debugPrint('OUTPUT PATH: $outputPath'); }, ); } @override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { if (Navigator.of(context).userGestureInProgress) { return false; } else { return true; } }, child: Scaffold( backgroundColor: Colors.white, appBar: AppBar( leading: IconButton( icon: const Icon( Icons.arrow_back_ios, color: Colors.white, ), onPressed: () { Navigator.pop(context); }, ), title: const Text( "Audio Trimmer", style: TextStyle(color: Colors.white), ), backgroundColor: Colors.blue, ), body: isLoading ? const Center(child: CircularProgressIndicator()) : Center( child: Container( padding: const EdgeInsets.only(bottom: 30.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ Center( child: Padding( padding: const EdgeInsets.all(8.0), child: TrimViewer( trimmer: _trimmer, viewerHeight: 100, viewerWidth: MediaQuery.of(context).size.width, durationStyle: DurationStyle.FORMAT_MM_SS, backgroundColor:Theme.of(context).primaryColor, barColor: Colors.white, durationTextStyle: TextStyle( color: Theme.of(context).primaryColor), allowAudioSelection: true, editorProperties: TrimEditorProperties( circleSize: 10, borderPaintColor: Colors.pink, borderWidth: 4, borderRadius: 5, circlePaintColor: Colors.pink.shade800, ), areaProperties: TrimAreaProperties.edgeBlur(blurEdges:true), onChangeStart: (value) => _startValue = value, onChangeEnd: (value) => _endValue = value, onChangePlaybackState: (value) { if (mounted) { setState(() => _isPlaying = value); } }, ), ), ), TextButton( child: _isPlaying ? Icon( Icons.pause, size: 80.0, color: Theme.of(context).primaryColor, ) : Icon( Icons.play_arrow, size: 80.0, color: Theme.of(context).primaryColor, ), onPressed: () async { bool playbackState = await _trimmer.audioPlaybackControl( startValue: _startValue, endValue: _endValue, ); setState(() => _isPlaying = playbackState); }, ), Visibility( visible: _progressVisibility, child: LinearProgressIndicator( backgroundColor: Theme.of(context).primaryColor.withOpacity(0.5), ), ), ElevatedButton( onPressed: _progressVisibility ? null : () =>_saveAudio(), child: const Text("SAVE"), ), ], ), ), ), ), ); } } |
As you can see in the output, we are picking an audio file, trimming, playing and saving it.
It allows you to remove unwanted parts from your audio clips and save the resulting part of the audio in various audio file formats such as MP3, WAV, AAC, FLAC, OGG, and WMA.
We have completed the implementation of how to create an audio trimmer in Flutter.
That’s it! You’ve successfully implemented easy audio trimmer in your Flutter app.
You can also check other blogs from here.
Hope this blog helped you to better understand the implementation of Easy Audio Trimmer In Flutter.
Thanks for reading this blog
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.