Asset file not found when initializing a video player

62 views Asked by At

I am trying to load this video assets/videos/heart_beat.mp4 but when passing its path to the initialization of VideoPlayerController, it throws this error:

Playback error
E/ExoPlayerImplInternal( 9149):   com.google.android.exoplayer2.ExoPlaybackException: Source error
.............
Caused by: com.google.android.exoplayer2.upstream.AssetDataSource$AssetDataSourceException: java.io.FileNotFoundException: flutter_assets/assets/videos/heat_beat.mp4

I think there is a problem where the last line indicates that it searches inside a folder called flutter_assets and then my path but I don't have a folder called flutter_assets. I tried creating it and moving my files into it but same issue.

class _HomeBodyState extends State<HomeBody> {
late ChewieController _chewieController;
  VideoPlayer videoPlayer = VideoPlayer(path: 'assets/videos/heat_beat.mp4');
  @override
  void initState() {
    videoPlayer.initializePlayers();
    _chewieController = videoPlayer.chewieController;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    streamListener(
        'Data/${User.uuid}/ECG',
        (DatabaseEvent data) =>
            onValueChangeCallback(data.snapshot.value.toString()));
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: widget.width * 0.02),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.only(top: widget.height * 0.1),
            child: Material(
              borderRadius: BorderRadius.circular(5),
              color: AppColors.secondaryContainer,
              child: SizedBox(
                  width: widget.width * 0.96,
                  height: widget.height * 0.4,
                  child: Chewie(controller: _chewieController)),
            ),
          ),
        ],
      ),
    );
  }

  void onValueChangeCallback(String value) {
    if (value != '' && value != '0') {
    } else {}
  }

  @override
  void dispose() {
    videoPlayer.dispose();
    _chewieController.dispose();
    super.dispose();
  }
}

My VideoPlayer:

import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class VideoPlayer {
  final String path;
  late VideoPlayerController videoPlayerController;
  late ChewieController chewieController;

  VideoPlayer({required this.path});

  void initializePlayers() {
    videoPlayerController = VideoPlayerController.asset(path);
    chewieController = ChewieController(
        videoPlayerController: videoPlayerController,
        allowMuting: true,
        autoPlay: false,
        allowedScreenSleep: false,
        autoInitialize: true,
        looping: true,
        showControls: false,
        zoomAndPan: false);
  }

  void dispose() {
    videoPlayerController.dispose();
    chewieController.dispose();
  }
}

pubspec:

flutter:
  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/images/
    - assets/videos/heart_beat.mp4
1

There are 1 answers

4
Raman Tank On

Share your pubspec.yaml file or add the path of the folder in the file Do like this in your pubspec.yaml file

Path should be define

I hope this will work out