Show list of all videos from internal storage

1.5k Views Asked by At

list all video files from a specific folder from internal storage and display them in flutter.

i want to create an app to list all videos in internal storage and show them in gridview and play that video

1

There are 1 best solutions below

0
Harsimran Singh On

To get all the files from the storage you can use path_provider flutter package. With this, get the folder path, extract all the files with an extension of .mp4 (video file), and store it in List.

List<FileSystemEntity> _files = [];

void _getFiles() async {
        Directory directory = await getExternalStorageDirectory();
        List<FileSystemEntity> files = directory.listSync().where((entity) => entity.path.endsWith('.mp4')).toList();
        setState(() {
          _files = files;
        });
      }

Then you can use video_player Flutter package to play the video.

GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        childAspectRatio: 1,
      ),
      itemCount: _files.length,
      itemBuilder: (BuildContext context, int index) {
        return GestureDetector(
          onTap: () {
            setState(() {
              _controller = VideoPlayerController.file(_files[index])
                ..initialize().then((_) {
                  _controller.play();
                });
            });
          },
          child: Card(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.video_library,
                  size: 50,
                ),
                SizedBox(height: 10),
                Text(_files[index].path.split('/').last),
              ],
            ),
          ),
        );
      },
    );