gpt4 book ai didi

Flutter Video Player On Click 传递 URL 动态并加载视频播放

转载 作者:行者123 更新时间:2023-12-05 02:09:23 32 4
gpt4 key购买 nike

sample screen of video player attached below 如何让视频播放器动态播放,同时通过传递视频 URL 来点击列表播放视频?

点击时我已经传递了 URL 并尝试重新初始化并开始播放它不起作用,状态没有改变

这是我的代码,

videoplayerscreen.dart,

class VideoPlayerScreen extends StatefulWidget {
int playBackTime;
int playBackTotalTime;
String setPlayTime;
String setPlayDuration;
double aspectRatio;
String videoUrl;
bool isForward;
bool isFullScreen;
bool allowFullScreen;
bool showControls;
bool isAutoPlay;
int startWithinSeconds;

VideoPlayerScreen({
Key key,
this.playBackTime = 0,
this.playBackTotalTime = 0,
this.setPlayTime = "00:00",
this.setPlayDuration = "00:00",
this.aspectRatio = 16 / 9,
this.videoUrl =
"",
this.isForward = true,
this.isFullScreen = false,
this.allowFullScreen = false,
this.showControls = true,
this.isAutoPlay = false,
this.startWithinSeconds = 0,
}) : super(key: key);

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

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
VideoPlayerController _controller;

void initPlayer() async {
_controller = VideoPlayerController.network(widget.videoUrl);
await _controller.initialize();
_controller.setLooping(true);
_controller.seekTo(Duration(seconds: widget.startWithinSeconds));
if (widget.isAutoPlay) {
_controller.play();
}
_controller.addListener(() {
setState(() {
widget.playBackTime = _controller.value.position.inSeconds;
widget.setPlayTime = timeFormatter(widget.playBackTime);
});
});
}

@override
void initState() {
initPlayer();
super.initState();
}

@override
void dispose() {
_controller.dispose();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}

在配置中,

video_player: ^0.10.5

调用其他类 CourseDetails.dart ,

             Container(
padding: EdgeInsets.only(left: 20.0, right: 20.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: _playUrl != null
? VideoPlayerScreen(videoUrl: _playUrl)
: Image.asset(
'assets/images/test_video_player_screen.png'),
),
)
GestureDetector(
**here i am changing the state **
onTap: () {
setState(() {
_playUrl = videoLists['course_video_url'];
});
},
child: Padding(
padding:
EdgeInsets.only(left: 10.0, right: 20.0, top: 2.0, bottom: 5.0),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
"${videoLists['sn_no']}",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black45,
fontFamily: 'Oswald-SemiBold'),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text(
"${videoLists['course_video_title']}",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black45,
fontFamily: 'Oswald-SemiBold'),
),
),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text(
"Video - ${videoLists['course_video_duration']}",
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Colors.grey,
fontFamily: 'Oswald-SemiBold'),
),
),
Padding(
padding: EdgeInsets.only(left: 5.0),
child: videoLists['course_watched']
? Image.asset(
'assets/images/green_tick_icon.png',
width: 12.0,
height: 12.0,
)
: null),
],
),
],
),
],
),
),
)

最佳答案

感谢大家的回复。我得到了解决方案:

Build 函数中,我检查了是否发生了任何 url 更改。如果发生变化,我会处理旧 Controller 并重新初始化视频 Controller 。然后,它对我来说很好用。

@override
Widget build(BuildContext context) {
if (currentObjectVideoUrl != widget.videoUrl) {
_controller.dispose();
initPlayer();
}
}

关于Flutter Video Player On Click 传递 URL 动态并加载视频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928309/

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