gpt4 book ai didi

android - 在 Flutter 中实现视频提要的最佳方式是什么?

转载 作者:行者123 更新时间:2023-12-03 04:13:37 24 4
gpt4 key购买 nike

我正在用 flutter 构建一个应用程序,其中包含一个视频源,就像在 TikTok 中找到的那样。您可以想象一个 ListView,您可以在其中滚动浏览一些视频(5-25 秒)。
视频存储在 Google Cloud Platform 中。
目前,包含超过 12 个甚至 9 个(在某些内存较低的设备上)的提要导致应用程序崩溃,这是可以理解的。
现在,作为一个业余爱好者,(几乎没有将 GCP 连接到 flutter 并连接它:p),我正在努力寻找一种能够尽可能流畅地滚动视频源的解决方案。我在想像带有可见度检测器的 Carousel 之类的东西,但我不确定如何重新加载已处理的视频。
我的视频播放器是“Video_player”包。
我对任何事情都持开放态度,并感谢您的帮助!
谢谢,

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用包https://pub.dev/packages/inview_notifier_list
示例代码将在指定区域内的屏幕上显示视频时自动播放视频
代码 fragment

InViewNotifierList(
scrollDirection: Axis.vertical,
initialInViewIds: ['0'],
isInViewPortCondition:
(double deltaTop, double deltaBottom, double viewPortDimension) {
return deltaTop < (0.5 * viewPortDimension) &&
deltaBottom > (0.5 * viewPortDimension);
},
children: List.generate(
30,
(index) {
return Container(
width: double.infinity,
height: 300.0,
alignment: Alignment.center,
margin: EdgeInsets.symmetric(vertical: 50.0),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final InViewState inViewState =
InViewNotifierList.of(context);

inViewState.addContext(context: context, id: '$index');

return AnimatedBuilder(
animation: inViewState,
builder: (BuildContext context, Widget child) {
return VideoWidget(
play: inViewState.inView('$index'),
url:
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
},
工作演示
enter image description here
完整代码
import 'package:flutter/material.dart';
import 'package:inview_notifier_list/inview_notifier_list.dart';
import 'package:video_player/video_player.dart';

class VideoWidget extends StatefulWidget {
final String url;
final bool play;

const VideoWidget({Key key, @required this.url, @required this.play})
: super(key: key);
@override
_VideoWidgetState createState() => _VideoWidgetState();
}

class _VideoWidgetState extends State<VideoWidget> {
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;

@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(widget.url);
_initializeVideoPlayerFuture = _controller.initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});

if (widget.play) {
_controller.play();
_controller.setLooping(true);
}
}

@override
void didUpdateWidget(VideoWidget oldWidget) {
if (oldWidget.play != widget.play) {
if (widget.play) {
_controller.play();
_controller.setLooping(true);
} else {
_controller.pause();
}
}
super.didUpdateWidget(oldWidget);
}

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

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return VideoPlayer(_controller);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}
}

class VideoList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
InViewNotifierList(
scrollDirection: Axis.vertical,
initialInViewIds: ['0'],
isInViewPortCondition:
(double deltaTop, double deltaBottom, double viewPortDimension) {
return deltaTop < (0.5 * viewPortDimension) &&
deltaBottom > (0.5 * viewPortDimension);
},
children: List.generate(
30,
(index) {
return Container(
width: double.infinity,
height: 300.0,
alignment: Alignment.center,
margin: EdgeInsets.symmetric(vertical: 50.0),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final InViewState inViewState =
InViewNotifierList.of(context);

inViewState.addContext(context: context, id: '$index');

return AnimatedBuilder(
animation: inViewState,
builder: (BuildContext context, Widget child) {
return VideoWidget(
play: inViewState.inView('$index'),
url:
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
},
);
},
),
);
},
),
),
Align(
alignment: Alignment.center,
child: Container(
height: 1.0,
color: Colors.redAccent,
),
)
],
);
}
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

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

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: VideoList()),
],
),
),
);
}
}

关于android - 在 Flutter 中实现视频提要的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62746677/

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