Maximum Duration of picked video using image picker plugin in Flutter - flutter

I'm using image picker plugin to pick videos from gallery in my flutter app. I want to set the maximum duration of the picked video to 30 seconds. The below code doesn't work even after setting the max duration. Is there any way to display an error or automatically trim the first 30secs if users pick a bigger video.
pickVideo(ImageSource src) async {
Navigator.pop(context);
final video = await ImagePicker().getVideo(
source: src,
maxDuration: Duration(seconds: 30),
);

I made a work around for this by throwing an error when a video longer than x seconds is selected. It looks as follows:
Future<void> pickVideo() async {
try {
final picker = ImagePicker();
var pickedFile = await picker.pickVideo(source: ImageSource.gallery, maxDuration: Duration(seconds: maxSeconds));
if (pickedFile == null) {
return;
}
VideoPlayerController testLengthController = new VideoPlayerController.file(File(pickedFile.path));//Your file here
await testLengthController.initialize();
if (testLengthController.value.duration.inSeconds > 60) {
pickedFile = null;
throw('we only allow videos that are shorter than 1 minute!');
} else {
setState(() {
videoFile = XFile(pickedFile.path);
_startVideoPlayer();
});
}
testLengthController.dispose();
} catch (e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
child: Text(e.toString()),
),
);
});
return;
}
}

Related

How to automatically ontap in flutter

Please help i have a video recording app which starts with a tap and a timer is also started with the same tap and after another tap it stops and timer reset . I want to stop and reset timer when the timer reaches 15 second how to do this please help
GestureDetector(
onTap:
() async {
if (isRecoring) {
stop();
XFile videopath =
await _cameraController.stopVideoRecording();
setState(() {
isRecoring = false;
Navigator.push(
context,
MaterialPageRoute(
builder: (builder) => VideoViewPage(
path: videopath.path,
)));
}
);
} else {
startTime();
await _cameraController.startVideoRecording();
setState(() {
isRecoring = true;
// ignore: use_build_context_synchronously
});
}
},`
i tried adding logical or like this ` if (isRecoring || timer == '15') {stop} but didnt work.
create a diffrent function with a if condition for stop even that didnt work
Firstly you need to define a Duration variable and a Timer to track the timer duration:
var videoDuration = const Duration();
Timer? autoStopRecordingTimer;
Then make a function which will stop the video recording:
void stopRecording()async{
stop();
XFile videopath = await _cameraController.stopVideoRecording();
setState(() {
isRecoring = false;
Navigator.push(
context,
MaterialPageRoute(
builder: (builder) => VideoViewPage(
path: videopath.path,
)));
}
);
}
When starting video recording, start the timer as well. Which will stop the recording if it has been 15 or more seconds:
autoStopRecordingTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
if(videoDuration.inSeconds>=15){
autoStopRecordingTimer?.cancel();
videoDuration = const Duration();
stopRecording();
} else {
videoDuration+=const Duration(seconds: 1);
}
});

how to handle the time lag after capturing the image ImagePicker Flutter

im learning flutter and now tried to capture the photo with ImagePicker package from the following method, but after I successfully capture the photo, there is always 1 sec until app get the data and jump to next page:
Future pickImage(ImageSource source) async {
try {
var image = await ImagePicker().pickImage(source: source);
if (image == null) return;
final imagePermanent = await saveImagePermanently(image.path);
selectedImage = File(imagePermanent.path);
isUploaded.value = !isUploaded.value;
update();
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future<File> saveImagePermanently(String imagePath) async {
final directory = await getApplicationDocumentsDirectory();
final name = basename(imagePath);
final image = File('${directory.path}/$name');
return File(imagePath).copy(image.path);
}
now my solution is adding a listener onInit when image is created with GetX:
#override
void onInit() {
super.onInit();
ever(isUploaded, (value) {
Get.to(
() => AddPersonProfileAddDetails(),
);
});
}
So is there a way to detect the status of capturing the image like finished/failed/progressing, thanks for any clue or let me know a better way to jump to next page after capturing the image, thanks a lot!

Flutter Firebase: storage on web with imagePicker

I have an implementation of imagePicker working with the mobile portion of my app, but i am currently trying to make it work on web too. Im getting an image with the following code:
ImagePicker _picker = ImagePicker();
final XFile? _image = await _picker.pickImage(
source: ImageSource.gallery,
imageQuality: 50,
);
if (_image == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('No image was selected.')));
}
if (_image != null) {
var f = await _image.readAsBytes();
StorageRepository()
.updateImage(user, _image, index);
}
Which leads to my confusion. My current storage method looks like this:
Future<void> uploadImageWeb(User user, XFile image, int? index) async {
try {
await storage
.ref('${user.id}/${image.name}')
.putFile(
File(image.path),
)
.then(
(p0) => FirestoreRepository().updateUserPictures(
user,
image.name,
index,
),
);
} catch (err) {
print(err);
}
}
Which obviously i cant use xfile, i have to use the Uint8List. But how do i read any data from that in a meaningful way to upload it to the storage bucket?
Thanks!

ImagePicker.platform shows warning - Flutter

I am using the following code to pick an image from user's gallery.
Future getImageFromGallery(BuildContext context) async {
await ImagePicker.platform()
.pickImage(source: ImageSource.gallery)
.then((image) {
if (image != null) {
_cropImage(image, context);
}
});
}
I am getting the following warning.
The member 'platform' can only be used within 'package:image_picker/image_picker.dart' or a test.
I'm not sure what the warning means. I tried looking it up but couldn't figure out the solution to resolve this warning.
Try below code hope its help to you
Declare File type form dart.io package
File? imagePicked;
Create Function for pick up the image
void gallaryImage() async {
final picker = ImagePicker();
final pickedImage = await picker.pickImage(
source: ImageSource.gallery,
);
final pickedImageFile = File(pickedImage!.path);
setState(() {
imagePicked = pickedImageFile;
});
}
Create your Widget
TextButton(
onPressed: gallaryImage,
child: Text(
'Gallery',
style: TextStyle(
color: Colors.black,
),
),
),
You can just change the code
ImagePicker.platform().pickImage(...)
to
ImagePicker().pickImage(...)
so
Future getImageFromGallery(BuildContext context) async {
await ImagePicker()
.pickImage(source: ImageSource.gallery)
.then((image) {
if (image != null) {
_cropImage(image, context);
}
});
}

Flutter Driver select date from date picker

I would like to select a date through date picker as part of a flutter driver test. however, I can't seem to figure out exactly how I would do this?
I've tried using a find.textandfind.bySemanticsLabel, and have tried but have had no luck thus far.
my code :
Future<void> executeStep() async {
await FlutterDriverUtils.waitForFlutter(world.driver);
NewOrderForm newOrderForm = NewOrderForm(world.driver);
await newOrderForm.setFieldKontrak();
//Open Date Picker
await newOrderForm.setDateKontrak();
//Select date 24
await driver.waitFor(find.bySemanticsLabel('24'));
await driver.tap(find.text('24'),timeout: Duration(seconds: 15));
await driver.waitFor(find.text('OK'));
await driver.tap(find.text('OK'));
await newOrderForm.setProyekField();
}
Screen capture :
I ran a sample test that selects a date from datepicker and it worked well. Below is what I did:
main.dart has a simple RaisedButton upon clicking on which DatePicker opens:
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
_showDatePicker();
},
child: Text('Click')
)
)
);
void _showDatePicker() async {
DateTime picked = await showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate: new DateTime(2019),
lastDate: new DateTime(2021)
);
if(picked != null) setState(() => _value = picked.toString());
}
Below is the flutter driver test that first identifies the RaisedButton -> taps on it -> finds the date to be selected -> taps on it -> taps OK
test('datepicker test', () async {
final dateButton = find.text('Click');
await driver.waitFor(dateButton);
await driver.tap(dateButton);
await driver.tap(find.text('15'));
await driver.tap(find.text('OK'));
print('October 15 selected and tapped OK');
});
Test result:
In the code you provided, you may try below snippet, ie, tap on 24 and directly tap on OK instead of telling driver to wait for OK button to find.
await driver.tap(find.text('24'));
await driver.tap(find.text('OK'));
Hope this helps you to resolve the issue.