gpt4 book ai didi

flutter - 如何使用视频播放器插件播放存储在作为数组引用的 Assets 文件夹中的视频列表

转载 作者:IT王子 更新时间:2023-10-29 07:06:11 28 4
gpt4 key购买 nike

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

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

class VideoApp extends StatefulWidget {
@override
_VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
var vidlist = [
'assets/videos/1.mp4',
'assets/videos/2.m4',
'assets/videos/3.mp4'
];
VideoPlayerController _controller;
bool _isPlaying = false;

@override
void initState() {
super.initState();
_controller = VideoPlayerController.asset(vidlist)
..addListener(() {
final bool isPlaying = _controller.value.isPlaying;
if (isPlaying != _isPlaying) {
setState(() {
_isPlaying = isPlaying;
});
}
})
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Video Demo',
home: Scaffold(
body: Center(
child: _controller.value.initialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Container(),
),
floatingActionButton: FloatingActionButton(
onPressed: _controller.value.isPlaying
? _controller.pause
: _controller.play,
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
),
);
}
}

这就是我所做的。我试图制作一个引用视频路径的字符串数组。但是,视频播放器 Controller 不能接受列表类型。那么如何播放本地存储的视频列表呢?我还尝试在 Controller 中使用 for 循环,但它不起作用。

最佳答案

我是 Flutter 的新手,所以我不确定这是一个干净的解决方案,但在我的情况下有效。根据你的变量

    var vdlist = [.......];
VideoPlayerController _controller;
//I am not using _isPlaying as variable
var currentIndexPosition = 0; // Position on the list

void initState(){
getFile();
}

void getFile(){
_controller = VideoPlayerController.asset(currentfile)
..addListener(() {

final bool isPlaying =_controller.value.position.inMilliseconds < _controller.value.duration.inMilliseconds;
// Tested with the following print
print("POSITION = " + _controller.value.position.inMilliseconds.toString() + " DURATION = " + _controller.value.duration.inMilliseconds.toString());

if (!isPlaying) {
setState(() {
nextVideo();
getFile();
});
}})
..initialize().then((_) {
setState(() {
_controller.play();
});
});

void nextVideo(){
if(currentIndexPosition < vdlist.length-1){
currentIndexPosition++;
}else{
currentIndexPosition = 0;
}

希望能有所帮助,也可能是获得改进解决方案的开始。

关于flutter - 如何使用视频播放器插件播放存储在作为数组引用的 Assets 文件夹中的视频列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com