Ask Your Question
4

Is it possible to play a video using the video_player library by utilizing raw bytes or files that are stored in memory rather than storage media?

asked 2023-06-09 02:15:38 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-09 02:35:01 +0000

devzero gravatar image

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);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-09 02:15:38 +0000

Seen: 11 times

Last updated: Jun 09 '23