Flutter Resumable Video Uploads
The FlutterResumableUploads
package provides functionality for uploading large video files in chunks, allowing for pausing and resuming uploads as needed. This is particularly useful for ensuring upload reliability over unstable network connections.
Features
- Chunked Uploads: Split large video files into manageable chunks for upload.
- Resumable Uploads: Pause and resume uploads to handle network interruptions.
- Progress Tracking: Monitor upload progress with customizable callbacks.
- Error Handling: Handle errors gracefully with customizable callbacks.
Installation
Add the package to your pubspec.yaml
file:
dependencies:
flutter_resumable_uploads: ^0.0.2
Then, run flutter pub get
to fetch the package.
Usage
Import the Package
import 'package:flutter_resumable_uploads/flutter_resumable_uploads.dart';
Exposed Methods
uploadVideo
This method initiates the upload process by splitting the video file into chunks and uploading each chunk sequentially.
Parameters:
file
(File): The video file to be uploaded.signedUrl
(String): The URL to obtain signed URLs for each chunk.chunkSize
(int?): The size of each chunk in megabytes (default is 15 MB).onProgress
(Function?): A callback function to track progress updates.onError
(Function?): A callback function to handle errors.
Example:
import 'dart:io';
import 'package:flutter_resumable_uploads/flutter_resumable_uploads.dart';
void main() async {
File videoFile = File('path_to_your_video_file.mp4');
String signedUrl = 'your_signed_url';
await FlutterResumableUploads.uploadVideo(
file: videoFile,
signedUrl: signedUrl,
chunkSize: 15,
onProgress: (progress) {
print('Progress: $progress');
},
onError: (error) {
print('Error: $error');
},
);
}
pauseUpload
Pauses the current upload process. Useful for handling temporary network interruptions or user-initiated pauses.
Example:
FlutterResumableUploads.pauseUpload();
resumeUpload
Resumes a previously paused upload process from where it left off.
Example:
FlutterResumableUploads.resumeUpload();
Example Usage
Here's a complete example demonstrating how to use the FlutterResumableUploads
package to upload a video, and pause/resume the upload.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_resumable_uploads/flutter_resumable_uploads.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UploadPage(),
);
}
}
class UploadPage extends StatefulWidget {
@override
_UploadPageState createState() => _UploadPageState();
}
class _UploadPageState extends State<UploadPage> {
String progress = 'Not Started';
void startUpload() async {
File videoFile = File('path_to_your_video_file.mp4');
String signedUrl = 'your_signed_url';
await FlutterResumableUploads.uploadVideo(
file: videoFile,
signedUrl: signedUrl,
chunkSize: 15,
onProgress: (prog) {
setState(() {
progress = prog;
});
},
onError: (error) {
setState(() {
progress = 'Error: $error';
});
},
);
}
void pauseUpload() {
FlutterResumableUploads.pauseUpload();
setState(() {
progress = 'Paused';
});
}
void resumeUpload() {
FlutterResumableUploads.resumeUpload();
setState(() {
progress = 'Resumed';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Video Upload'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Progress: $progress'),
ElevatedButton(
onPressed: startUpload,
child: Text('Start Upload'),
),
ElevatedButton(
onPressed: pauseUpload,
child: Text('Pause Upload'),
),
ElevatedButton(
onPressed: resumeUpload,
child: Text('Resume Upload'),
),
],
),
),
);
}
}
Testing
This package includes a suite of tests to ensure functionality. You can run the tests using the following command:
flutter test test/flutter_resumable_uploads_test.dart
Contributions
Contributions are welcome! Please submit a pull request or open an issue to discuss your changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.