Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.0+18

* Fix video play in example and update video_player plugin dependency.

## 0.6.0+17

* iOS: Fix a crash when user captures image from the camera with devices under iOS 11.
Expand Down
97 changes: 46 additions & 51 deletions packages/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,76 +39,72 @@ class _MyHomePageState extends State<MyHomePage> {
VideoPlayerController _controller;
String _retrieveDataError;

Future<void> _playVideo(File file) async {
if (file != null && mounted) {
await _disposeVideoController();
_controller = VideoPlayerController.file(file);
await _controller.setVolume(1.0);
await _controller.initialize();
await _controller.setLooping(true);
await _controller.play();
setState(() {});
}
}

void _onImageButtonPressed(ImageSource source) async {
if (_controller != null) {
_controller.setVolume(0.0);
_controller.removeListener(_onVideoControllerUpdate);
await _controller.setVolume(0.0);
}
if (isVideo) {
ImagePicker.pickVideo(source: source).then((File file) {
if (file != null && mounted) {
setState(() {
_controller = VideoPlayerController.file(file)
..addListener(_onVideoControllerUpdate)
..setVolume(1.0)
..initialize()
..setLooping(true)
..play();
});
}
});
final File file = await ImagePicker.pickVideo(source: source);
await _playVideo(file);
} else {
try {
_imageFile = await ImagePicker.pickImage(source: source);
setState(() {});
} catch (e) {
_pickImageError = e;
}
setState(() {});
}
}

void _onVideoControllerUpdate() {
setState(() {});
}

@override
void deactivate() {
if (_controller != null) {
_controller.setVolume(0.0);
_controller.removeListener(_onVideoControllerUpdate);
_controller.pause();
}
super.deactivate();
}

@override
void dispose() {
_disposeVideoController();
super.dispose();
}

Future<void> _disposeVideoController() async {
if (_controller != null) {
_controller.dispose();
await _controller.dispose();
_controller = null;
}
super.dispose();
}

Widget _previewVideo(VideoPlayerController controller) {
Widget _previewVideo() {
final Text retrieveError = _getRetrieveErrorWidget();
if (retrieveError != null) {
return retrieveError;
}
if (controller == null) {
if (_controller == null) {
return const Text(
'You have not yet picked a video',
textAlign: TextAlign.center,
);
} else if (controller.value.initialized) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: AspectRatioVideo(controller),
);
} else {
return const Text(
'Error Loading Video',
textAlign: TextAlign.center,
);
}
return Padding(
padding: const EdgeInsets.all(10.0),
child: AspectRatioVideo(_controller),
);
}

Widget _previewImage() {
Expand Down Expand Up @@ -137,20 +133,15 @@ class _MyHomePageState extends State<MyHomePage> {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.video) {
isVideo = true;
_controller = VideoPlayerController.file(response.file)
..addListener(_onVideoControllerUpdate)
..setVolume(1.0)
..initialize()
..setLooping(true)
..play();
} else {
isVideo = false;
if (response.type == RetrieveType.video) {
isVideo = true;
await _playVideo(response.file);
} else {
isVideo = false;
setState(() {
_imageFile = response.file;
}
});
});
}
} else {
_retrieveDataError = response.exception.code;
}
Expand All @@ -175,9 +166,7 @@ class _MyHomePageState extends State<MyHomePage> {
textAlign: TextAlign.center,
);
case ConnectionState.done:
return isVideo
? _previewVideo(_controller)
: _previewImage();
return isVideo ? _previewVideo() : _previewImage();
default:
if (snapshot.hasError) {
return Text(
Expand All @@ -193,7 +182,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
},
)
: (isVideo ? _previewVideo(_controller) : _previewImage()),
: (isVideo ? _previewVideo() : _previewImage()),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
Expand Down Expand Up @@ -289,6 +278,12 @@ class AspectRatioVideoState extends State<AspectRatioVideo> {
controller.addListener(_onVideoControllerUpdate);
}

@override
void dispose() {
controller.removeListener(_onVideoControllerUpdate);
super.dispose();
}

@override
Widget build(BuildContext context) {
if (initialized) {
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Demonstrates how to use the image_picker plugin.
author: Flutter Team <[email protected]>

dependencies:
video_player: 0.5.2
video_player: 0.10.1+5
flutter:
sdk: flutter
image_picker:
Expand Down
4 changes: 2 additions & 2 deletions packages/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors:
- Rhodes Davis Jr. <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker

version: 0.6.0+17
version: 0.6.0+18

flutter:
plugin:
Expand All @@ -19,7 +19,7 @@ dependencies:
sdk: flutter

dev_dependencies:
video_player: 0.5.2
video_player: 0.10.1+5
flutter_test:
sdk: flutter

Expand Down