flutter_audio_trimmer 0.0.1 copy "flutter_audio_trimmer: ^0.0.1" to clipboard
flutter_audio_trimmer: ^0.0.1 copied to clipboard

Trimming an audio file means cutting a portion of the audio from the beginning or end of the file or removing some part from the middle.

example/lib/main.dart

import 'dart:io';

import 'package:flutter_audio_trimmer/flutter_audio_trimmer.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File? _file;
  File? _outputFile;

  Future<void> _onPickAudioFile() async {
    try {
      Permission permission =
          Platform.isAndroid ? Permission.storage : Permission.photos;
      if (await permission.request().isGranted) {
        FilePickerResult? result = await FilePicker.platform.pickFiles(
          allowMultiple: false,
          type: FileType.any,
        );

        if (result != null && result.files.isNotEmpty) {
          setState(() {
            _file = File(result.files.first.path ?? '');
          });
        }
      }
    } catch (e) {
      _showSnackBar(e.toString());
    }
  }

  Future<void> _onTrimAudioFile() async {
    try {
      if (_file != null) {
        Directory directory = await getApplicationDocumentsDirectory();

        File? trimmedAudioFile = await AudioTrimmer.trim(
          inputFile: _file!,
          outputDirectory: directory,
          fileName: DateTime.now().millisecondsSinceEpoch.toString(),
          fileType: AudioFileType.m4a,
          time: AudioTrimTime(
            start: const Duration(seconds: 50),
            end: const Duration(seconds: 100),
          ),
        );
        setState(() {
          _outputFile = trimmedAudioFile;
        });
      } else {
        _showSnackBar('Select audio file for trim');
      }
    } on AudioTrimmerException catch (e) {
      _showSnackBar(e.message);
    } catch (e) {
      _showSnackBar(e.toString());
    }
  }

  void _showSnackBar(String message) {
    SnackBar snackBar = SnackBar(content: Text(message));
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Plugin example app')),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Center(
          child: Column(
            children: [
              if (_file != null) Text(_file?.path ?? ''),
              ElevatedButton(
                onPressed: _onPickAudioFile,
                child: const Text('Pick Audio File'),
              ),
              const SizedBox(height: 20),
              if (_outputFile != null) Text(_outputFile?.path ?? ''),
              ElevatedButton(
                onPressed: _onTrimAudioFile,
                child: const Text('Trim Audio'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
5
likes
0
pub points
77%
popularity

Publisher

verified publishervdeveloper.tech

Trimming an audio file means cutting a portion of the audio from the beginning or end of the file or removing some part from the middle.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_audio_trimmer