【问题标题】:Flutter display video like imageFlutter 显示视频像图像
【发布时间】:2017-12-15 14:36:13
【问题描述】:

我正在为 Flutter 修改 image_picker 插件。当我将数据返回到颤振应用程序时,我已经接受了所有工作,我需要帮助来弄清楚如何将视频显示给用户,类似于我使用 Image.file 拍摄图片的方式。然后他们可以点击上传,当我检索需要播放时,查看了video_player插件,但真的只关心让他们使用默认平台播放器,但如果我能让它工作那么插件就可以了,但没有到目前为止,即使看到视频也很幸运。从 image_picker 插件返回的数据是一个文件,我确定它不是一个图像并从那里去。

任何帮助都会很棒,如果有帮助,我可以显示任何代码。

谢谢

【问题讨论】:

  • 不确定您要达到的目标。想要扩展 image_picker 以选择视频吗?一段代码或 github url 将有助于解决。
  • 我一定会在完成后上传所有代码。我的目的是我的应用程序中的聊天功能,用户可以在其中上传图片或视频。图片是直截了当的,但视频不是原生的,所以我试图在用户上传之前向他们显示它,然后在检索到它时播放。
  • image_picker 已经挑选了视频。 ImagePicker.pickVideo 返回 File 就像 ImagePicker.pickImage 一样。我猜原发帖人希望能够做到Image.fromFile(videoFile),这是行不通的。
  • 当我写这篇文章的时候,他们在插件中没有那个功能。

标签: flutter


【解决方案1】:

幸好image_picker 现在正在挑选视频。

如果您要运行 sample app,它的外观是这样的:

这是另一个示例,您可以使用image_picker 播放和暂停从本地文件上传的视频,并使用video_player 播放视频,如Flutter documentation 中所推荐的那样。

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:video_player/video_player.dart';

void main() => runApp(VideoPlayerApp());

class VideoPlayerApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Player Demo',
      home: VideoPlayerScreen(),
    );
  }
}

class VideoPlayerScreen extends StatefulWidget {
  VideoPlayerScreen({Key key}) : super(key: key);

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

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;

  File videoFile;

  @override
  void initState() {
    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    super.initState();
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sample Video Player Demo'),
      ),
      // Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      body: Column(
        children: <Widget>[
          Visibility(
            visible: _controller != null,
            child: FutureBuilder(
              future: _initializeVideoPlayerFuture,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  // If the VideoPlayerController has finished initialization, use
                  // the data it provides to limit the aspect ratio of the video.
                  return AspectRatio(
                    aspectRatio: _controller.value.aspectRatio,
                    // Use the VideoPlayer widget to display the video.
                    child: VideoPlayer(_controller),
                  );
                } else {
                  // If the VideoPlayerController is still initializing, show a
                  // loading spinner.
                  return Center(child: CircularProgressIndicator());
                }
              },
            ),
          ),
          Center(
            child: MaterialButton(
              hoverColor: Colors.blue,
              child: Text("Upload Video"),
              onPressed: () {
                getVideo();
              },
            ),
          ),
        ],
      ),
      floatingActionButton: _controller == null
          ? null
          : FloatingActionButton(
              onPressed: () {
                // Wrap the play or pause in a call to `setState`. This ensures the
                // correct icon is shown.
                setState(() {
                  // If the video is playing, pause it.
                  if (_controller.value.isPlaying) {
                    _controller.pause();
                  } else {
                    // If the video is paused, play it.
                    _controller.play();
                  }
                });
              },
              // Display the correct icon depending on the state of the player.
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
              ),
            ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Future getVideo() async {
    var picker;
    Future<File> _videoFile =
        ImagePicker.pickVideo(source: ImageSource.gallery);
    _videoFile.then((file) async {
      setState(() {
        videoFile = file;
        _controller = VideoPlayerController.file(videoFile);

        // Initialize the controller and store the Future for later use.
        _initializeVideoPlayerFuture = _controller.initialize();

        // Use the controller to loop the video.
        _controller.setLooping(true);
      });
    });
  }
}

实际输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2019-09-06
    • 2020-05-11
    • 2017-11-09
    相关资源
    最近更新 更多