Play local videos in Flutter

5.3k Views Asked by At

I need to play a asset video in my app but the vide player plugin keeps on buffering but didn't play the video. But if I use network videos then the code works perfectly.

Here is my code,

class LandscapePlayer extends StatefulWidget {
  @override
  _LandscapePlayerState createState() => _LandscapePlayerState();
}

class _LandscapePlayerState extends State<LandscapePlayer> {
  FlickManager flickManager;

  @override
  void initState() {
    super.initState();
    flickManager = FlickManager(videoPlayerController:
         VideoPlayerController.asset('videos/first.mp4',),);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FlickVideoPlayer(
        flickManager: flickManager,
        preferredDeviceOrientation: [
          DeviceOrientation.landscapeRight,
          DeviceOrientation.landscapeLeft
        ],
        systemUIOverlay: [],
        flickVideoWithControls: FlickVideoWithControls(
          controls: LandscapePlayerControls(),
        ),
      ),
    );
  }
}

I have added the video file as a dependency in pubspec.yaml file. I don't know why it works for network files but not for asset file.

4

There are 4 best solutions below

0
On BEST ANSWER

I solved this issue. video_player plugin uses Exo player plugin in android which is not working correctly in some devices(due to decoders or something). An easy alternative is to go by native_video_view plugin.

0
On

Try specifying the file path like this: "file://foldername/file.extension"

0
On

You can also use VideoPlayerController, to add this run

flutter pub add video_player

create a folder for video files named it videos and mention this folder in pubspec.yaml file under assets like this:

assets:
 - images/
 - videos/

and then initialise player in initState method follow steps mentioned here: Implement video player in flutter

0
On

I had the same issue as well. But one thing I noticed was, some sample videos from the Internet weren't working. The error I was getting was "Video codec error". So, I downloaded the working ones and put them in the assets folder. To my surprise, those files worked. After digging a lot, and a lot of trial-error, running this command to scale down the resolution on the files that didn't work, did the trick.

ffmpeg -i source_file.mp4 -vf "scale=640:360" -c:v libx264 -preset slow -crf 22 -c:a copy target_file.mp4