Implement Audio Trimmer In Flutter

Implement Audio Trimmer In Flutter

Implementation:

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies:
flutter:
sdk: flutter
sqflite: ^2.2.8+4
path_provider: ^2.1.1
supplier: ^6.0.1

Step 2: Import

import 'package deal:signature/signature.dart';

Step 3: Run flutter packages get within the root listing of your app.

Step 4: While working on the Android platform if it provides an error that minSdkVersion must be 24, or on the iOS platform that the Podfile platform model ought to be 11

How to implement code in dart file :

You have to implement it in your code respectively:

Create a brand new dart file referred to as important.dart contained in the lib folder.

In this dart file, we’ll create a brand new class AudioTrimmerDemo(). In this class, will add an ElevatedButton(). In this button, we’ll add the textual content “Select File” to its youngster perform, and on the onPressed perform, we’ll add the pick-up audio file perform.

Center(
youngster: ElevatedButton(
model:
ElevatedButton.styleFrom(backgroundColor: Colors.teal.shade50),
onPressed: () async {
FilePickerResult? outcome = await FilePicker.platform.chooseFiles(
sort: FileSort.audio,
allowCompression: false,
);
if (outcome != null) {
File file = File(outcome.information.single.path!);
Navigator.of(context).push(
MaterialPageRoute(builder: (context) {
return AudioTrimmerViewDemo(file);
}),
);
}
},
youngster: const Text(
"Select File",
model: TextModel(coloration: Colors.teal),
)),
),

When we run the appliance, we must get the display’s output just like the beneath display seize.

Output

In the identical dart file, we’ll create one other new class AudioTrimmerViewDemo().

In this class, we’ll create a last Trimmer variable which is _trimmer is the same as Trimmer(). We will create two double variables _startValue and _endValue equal to 0.0. Also, we’ll create a three-bool variable was _isPlaying, _progressVisibility, and isLoading equal to false.

last Trimmer _trimmer = Trimmer();
double _startValue = 0.0;
double _endValue = 0.0;
bool _isPlaying = false;
bool _progressVisibility = false;
bool isLoading = false;

We will create an initState() methodology. In this methodology, we’ll add a _loadAudio() methodology. In this methodology, we’ll load the trimmer audio information.

@override
void initState() {
tremendous.initState();
 _loadAudio();
}
void _loadAudio() async {
setState(() {
isLoading = true;
});
await _trimmer.loadAudio(audioFile: widget.file);
setState(() {
isLoading = false;
});
}

We will create a _saveAudio methodology. In this methodology, we’ll save trimmed audio information.

_saveAudio() {
setState(() {
_progressVisibility = true;
});
 _trimmer.saveTrimmedAudio(
startValue: _startValue,
endValue: _endValue,
audioFileTitle: DateTime.now().millisecondsSinceEpoch.toString(),
onSave: (outputPath) {
setState(() {
_progressVisibility = false;
});
debugPrint('OUTPUT PATH: $outputPath');
},
);
}

In the physique half, we’ll create an audio trimmer view utilizing the TrimViewer() methodology. In this methodology, we’ll set backgroundColor, barColor, viewerHeight, onChangeStart, and so forth.

Center(
youngster: Padding(
padding: const EdgeInunits.all(8.0),
youngster: TrimViewer(
trimmer: _trimmer,
viewerHeight: 100,
viewerWidth: MediaQuery.of(context).dimension.width,
durationStyle: DurationStyle.FORMAT_MM_SS,
backgroundColor:Colors.teal,
barColor: Colors.white,
lengthTextModel: const TextModel(
coloration: Colors.teal),
permitAudioSelection: true,
editorProperties: const TrimEditorProperties(
circleSize: 10,
borderPaintColor: Colors.pink,
borderWidth: 4,
borderRadius: 5,
circlePaintColor: Colors.redAccent,
),
areaProperties:
TrimAreaProperties.edgeBlur(blurEdges:true),
onChangeStart: (worth) => _startValue = worth,
onChangeEnd: (worth) => _endValue = worth,
onChangePlaybackState: (worth) {
if (mounted) {
setState(() => _isPlaying = worth);
}
},
),
),
),

Also, we’ll add the TextButton() methodology. In this methodology, we’ll add play and pause button capabilities.

TextButton(
youngster: _isPlaying
? const Icon(
Icons.pause,
dimension: 80.0,
coloration: Colors.teal,
)
: const Icon(
Icons.play_arrow,
dimension: 80.0,
coloration: Colors.teal,
),
onPressed: () async {
bool playbackState =
await _trimmer.audioPlaybackControl(
startValue: _startValue,
endValue: _endValue,
);
setState(() => _isPlaying = playbackState);
},
),

Also, we’ll add the Visibility() methodology. In this methodology, we’ll add LinearProgressIndicator(). This methodology will work on the save button solely. When the consumer presses save the audio then the LinearProgressIndicator will likely be proven.

Visibility(
seen: _progressVisibility,
youngster: LinearProgressIndicator(
backgroundColor:
Theme.of(context).primaryColor.withOpacity(0.5),
),
),
ElevatedButton(
model:
ElevatedButton.styleFrom(backgroundColor: Colors.teal.shade50),
onPressed:
_progressVisibility ? null : () =>_saveAudio(),
youngster: const Text("SAVE",model: TextModel(coloration: Colors.teal)),
),

When we run the appliance, we must get the display’s output just like the beneath display seize.

Output

HI-FI News

by way of Mobile App Development on Medium https://ift.tt/Ozh4kZJ

December 15, 2023 at 01:23PM