Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to play a video using the videoplayer library by utilizing raw bytes or files that are stored in memory rather than storage media. The videoplayer library supports playing videos from various sources such as local files, network URLs, and bytes. To play a video from bytes, you can use the MemoryVideo class provided by the library. The MemoryVideo class takes a Uint8List as input and returns a VideoPlayerController instance which can be used to control the video playback. Here is an example of how to play a video from bytes using the video_player library:

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class MemoryVideoPlayer extends StatefulWidget {
  final Uint8List videoBytes;

  MemoryVideoPlayer({required this.videoBytes});

  @override
  _MemoryVideoPlayerState createState() => _MemoryVideoPlayerState();
}

class _MemoryVideoPlayerState extends State<MemoryVideoPlayer> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.memory(widget.videoBytes);
    _controller.initialize().then((_) {
      setState(() {});
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Memory Video Player'),
      ),
      body: _controller.value.isInitialized
          ? AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            )
          : Center(
              child: CircularProgressIndicator(),
            ),
    );
  }
}

You can then use the MemoryVideoPlayer widget to play a video from bytes as follows:

Uint8List videoBytes = // get the video bytes from somewhere
MemoryVideoPlayer(videoBytes: videoBytes);