How to Stop Video Recording After some Seconds? - flutter

I'm recording video using image_picker. I want to stop that recording after 20seconds. Is there any way to stop recording after 20seconds.
_pickVideoFromCamera() async {
File video = await ImagePicker.pickVideo(source: ImageSource.camera);
_cameraVideo = video;
_cameraVideoPlayerController = VideoPlayerController.file(_cameraVideo)
..initialize().then((_) {
_cameraVideoPlayerController.play();
setState(() {});
});
}

You can use Future.delayed
_pickVideoFromCamera() async {
File video = await ImagePicker.pickVideo(source: ImageSource.camera);
_cameraVideo = video;
_cameraVideoPlayerController = VideoPlayerController.file(_cameraVideo)
..initialize().then((_) {
_cameraVideoPlayerController.play();
setState(() {});
});
Future.delayed(Duration(seconds: 20)).then((_) { // camera stop process });
}

Related

flutter video player not showing video

In my app I have a video running in the background.
I have a parameter page with a filepicker button which saves the path of the selected video in sharedpreferences
Future<File> _pickVideo() async {
final result = await FilePicker.platform.pickFiles(type: FileType.video);
File file = File(result!.files.single.path ?? '');
if (file != null) {
setState(() {
pickedVideo = file;
print('video $pickedVideo');
});
}
return file;}
In the video player page i retrieve the sharedpreferences path and I can display it in a Text widget
Text(selectedVideoValue ?? '',)
when I want to use it in the video player it doesn't work.
am i doing something wrong?
String? selectedVideoValue;
if (selectedVideoValue != null) {
controller = VideoPlayerController.file(File('{$selectedVideoValue}'))
..addListener(() => setState(() {}))
..initialize().then((_) {
controller.setVolume(0.0);
controller.play();
controller.setLooping(true);
setState(() {});
});
} else {
controller =
VideoPlayerController.asset("assets/movies/STYLEARCHIGRAPHIQUE.mp4")
..addListener(() => setState(() {}))
..initialize().then((_) {
controller.setVolume(0.0);
controller.play();
controller.setLooping(true);
setState(() {});
});
}
Thanks for your help
Below Code set into initState() may be working
controller = VideoPlayerController.file(File('{$selectedVideoValue}'))
..addListener(() => setState(() {}))
..initialize().then((_) {
controller.setVolume(0.0);
controller.play();
controller.setLooping(true);
setState(() {});
});
} else {
controller =
VideoPlayerController.asset("assets/movies/STYLEARCHIGRAPHIQUE.mp4")
..addListener(() => setState(() {}))
..initialize().then((_) {
controller.setVolume(0.0);
controller.play();
controller.setLooping(true);
setState(() {});
});
}```

Flutter_sound doesn't work at the first time but after recording voice on iOS

I'm building a flutter app and inside I use flutter_sound for voice recording and playing.
It works totally fine with Android devices but not with iOS device.
Right after building the app, I play a sound fetched from firebase but there was no sound at all. However, after I record a new voice, the previously fetched sound can play properly.
This is the code for playing sound
void startPlay() {
isPlaying = true;
_player = FlutterSoundPlayer();
_player!.openPlayer();
_player!.startPlayer(
fromDataBuffer: widget.bodyBytes,
codec: Codec.pcm16,
sampleRate: 48000,
whenFinished: () {
isPlaying = false;
if(mounted) {
setState(() {});
}
});
setState(() {});
}
This is the code for recording
Future<bool> record() async {
var status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
status = await Permission.microphone.request();
return false;
}
recordingDataController = StreamController<Food>.broadcast();
// write to data
_sink = File(_tempFilePath!).openWrite();
recordingDataSubscriptor = recordingDataController!.stream.listen((buffer) {
if (buffer is FoodData) {
_sink.add(buffer.data!);
}
});
await _recorder!.openRecorder();
await _recorder!.startRecorder(
toStream: recordingDataController!.sink,
codec: Codec.pcm16,
numChannels: 1,
sampleRate: 48000,
);
return true;
}
Please help me, Love you all <3

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!

AssetsAudioPlayer the operation could not be completed iOS

I'm using Flutter Sound Recorder as recorder for my audio files and I'm trying to open the audio file with AssetsAudioPlayer but I'm getting this error:
url: file:///var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav
"playback failed"
2022-06-23 23:45:04.546002-0300 Runner[412:13249] flutter: PlatformException(PLAY_ERROR, Cannot play /var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav, The operation could not be completed, null)
2022-06-23 23:45:04.546905-0300 Runner[412:13249] [VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: PlatformException(PLAY_ERROR, Cannot play /var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav, The operation could not be completed, null)
I've tried to use Audio Players but I got an error too
"iOS => call setVolume, playerId 57c0b5cc-3c75-4f8f-bf99-ad8ddbcb7709"
"iOS => call setUrl, playerId 57c0b5cc-3c75-4f8f-bf99-ad8ddbcb7709"
""
2022-06-23 18:27:00.168194-0300 Runner[16264:695114] flutter: AVPlayerItem.Status.failed
Any idea of what can I Do?
My recording settings:
Future<void> _onRecordButtonPressed() async {
if(_isRecording){
await _myRecorder?.stopRecorder();
_isRecording = false;
_isRecorded = true;
}else{
_isRecorded = false;
_isRecording = true;
await _startRecording();
}
setState((){
});
}
My AssetsAudioPlayer settings:
_onPlayButtonPressed() async {
if(!_isPlaying) {
_isPlaying = true;
await audioPlayer2.onPlayerError.listen((event) {
print(event);
});
await _assetsAudioPlayer.open(Audio.file(FilePathAndroidBitmap));
}else{
audioPlayer2.pause();
_isPlaying = false;
}
setState((){});
}

flutter sounds unable to play audio file

I am using flutter_sounds package to play audio file.
My audio file exists and is valid file.
For some unknown reason below code is not working and unable to play the audio.
FlutterSoundPlayer _mPlayer = FlutterSoundPlayer();
try {
var t = await _mPlayer.openAudioSession();
print('after open');
print('_mPlayerIsInited');
setState(() async {
_isPlayerToucher = true;
_mPlayerIsInited = true;
_mPlayerisPaused = false;
});
} catch (e) {
print('ERROR');
print(e.toString());
}
await _mPlayer.startPlayer(
fromURI: widget.content,
whenFinished: () async {
await _mPlayer.stopPlayer();
_mPlayer.closeAudioSession();
setState(() {
_mPlayerIsInited = false;
});
});
I am unable to see the print 'after open'
i see below in console.
I/flutter ( 7834): FS:---> openAudioSession
I recommend you to use another package audioplayers
AudioCache _audioCache;
_audioCache = AudioCache(
prefix: "audio/",
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
_audioCache.play('open-up.mp3');