I am have issues with my my video-player all issue lead to this point but I don't know what to do once I click on the play button I get this error please I need help with the initialization
Reload already in progress, ignoring request
Restarted application in 1,023ms.
════════ Exception caught by widgets library ═══════════════════════════════════
The following LateError was thrown building Video(dirty, state: _VideoState#4a5db):
LateInitializationError: Field '_chewieController#50055179' has not been initialized.
The relevant error-causing widget was
Video
package:goodwork/allscreens/videoplayer.dart:69
When the exception was thrown, this was the stack
#0 _VideoState._chewieController (package:goodwork/allscreens/videoplayer.dart)
package:goodwork/allscreens/videoplayer.dart:1
enter image description here
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class Video extends StatefulWidget {
final VideoPlayerController videoPlayerController;
final bool loop;
Video({
required this.videoPlayerController,
required this.loop,
Key? key,
}) : super(key: key);
#override
_VideoState createState() => _VideoState();
}
class _VideoState extends State<Video> {
late ChewieController _chewieController;
#override
void iniState() {
super.initState();
_chewieController = ChewieController(
videoPlayerController: widget.videoPlayerController,
looping: widget.loop,
aspectRatio: 16 / 9,
autoInitialize: true);
}
#override
Widget build(BuildContext context) {
return Chewie(
controller: _chewieController,
);
}
#override
void dispose() {
super.dispose();
widget.videoPlayerController.dispose();
_chewieController.dispose();
}
}
class Videoplayers extends StatefulWidget {
final String videodata;
Videoplayers({required this.videodata});
#override
_VideoplayersState createState() => _VideoplayersState();
}
class _VideoplayersState extends State<Videoplayers> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: const Text(
"Video Player",
style: TextStyle(color: Colors.black),
),
leading: const Image(
image: AssetImage("images/logo.png"),
)),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Video(
loop: true,
videoPlayerController:
VideoPlayerController.network(widget.videodata),
),
),
),
);
}
}
You wrote iniState instead of initState. And after correcting Restart the app, not Reload.
Related
I am new to the world of Flutter, I am creating a video player with the following libraries:
-Video_Player
-chewie
The problem is that I follow the instructions in the documentation and also in several videos on YouTube, only that the video plays in the background (audio is heard), but the video does not appear. For more information about my problem, I attach my code, I thank you in advance for all the help provided.
import 'package:flutter/material.dart';
import 'package:chewie/chewie.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Euforia',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
// ignore: library_private_types_in_public_api
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late VideoPlayerController controller;
ChewieController? chewieController;
Future<void> loadVideoPlayer() async {
controller = VideoPlayerController.network(
"https://download1486.mediafire.com/xtpol73k5d0g/6udcu6b0onjnuv5/Santa+RM+-+Mucho+Para+M%C3%AD+%28Ft.+Franco+Escamilla%29+%5BVideo+Oficial%5D_2.mp4");
await Future.wait([controller.initialize()]);
chewieController = ChewieController(
videoPlayerController: controller, autoPlay: true, looping: false);
}
#override
void initState() {
super.initState();
loadVideoPlayer();
}
#override
void dispose() {
super.dispose();
controller.dispose();
chewieController!.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Hola Mundo')),
body: Column(
children: [
Expanded(
child: Center(
child: chewieController != null &&
chewieController!.videoPlayerController.value.isInitialized
? Chewie(controller: chewieController!)
: Center(
child: Column(
children: const [
CircularProgressIndicator(),
SizedBox(
height: 60.0,
),
Text("Cargando")
],
),
),
))
],
),
);
}
}
I also provide the official page
https://pub.dev/packages/chewie
After initializing controller and video you need to update screen state. Try this in Init Code:
#override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
await loadVideoPlayer();
setState(() {});
});
}
LateInitializationError: Field '_latestValue#875205881' has not been initialized
Error image
I do not know what is the cause of the problem I tried to put ? Instead of late but there are still mistakes
Help me, thanks
video_screen.dart
import 'package:flutter/material.dart';
import '../screens/chewie_list_item.dart';
import 'package:chewie/chewie.dart';
import 'package:video_player/video_player.dart';
class VideoScreen extends StatefulWidget {
const VideoScreen({Key? key}) : super(key: key);
#override
_VideoScreenState createState() => _VideoScreenState();
}
class _VideoScreenState extends State<VideoScreen> {
ChewieController? _chewieController;
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
ChewieListItem(
videoPlayerController: VideoPlayerController.asset(
'videos/video1.mp4',
),
looping: true,
),
ChewieListItem(
videoPlayerController: VideoPlayerController.asset(
'videos/video2.mp4',
),
looping: true,
),
],
),
);
}
}
chewie_list_item.dart
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class ChewieListItem extends StatefulWidget {
// This will contain the URL/asset path which we want to play
final VideoPlayerController videoPlayerController;
final bool looping;
ChewieListItem({
required this.videoPlayerController,
required this.looping,
Key? key,
}) : super(key: key);
#override
_ChewieListItemState createState() => _ChewieListItemState();
}
class _ChewieListItemState extends State<ChewieListItem> {
late ChewieController _chewieController;
#override
void initState() {
super.initState();
// Wrapper on top of the videoPlayerController
_chewieController = ChewieController(
videoPlayerController: widget.videoPlayerController,
aspectRatio: 16 / 9,
// Prepare the video to be played and display the first frame
autoInitialize: true,
looping: widget.looping,
// Errors can occur for example when trying to play a video
// from a non-existent URL
errorBuilder: (context, errorMessage) {
return Center(
child: Text(
errorMessage,
style: TextStyle(color: Colors.white),
),
);
},
);
}
#override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: Chewie(
controller: _chewieController,
),
);
}
#override
void dispose() {
super.dispose();
// IMPORTANT to dispose of all the used resources
widget.videoPlayerController.dispose();
_chewieController.dispose();
}
}
I was trying to create a videoplayer in a stateless widget in flutter. I could not do it.... this is the code I used ...Is it possible to create a video player in a stateless widget?
class VideoWidget extends StatelessWidget {
VideoWidget({Key? key}) : super(key: key);
final videoController =
VideoPlayerController.network('https://youtu.be/_EoLNs5m-7Y?t=4')
..initialize();
#override
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery.of(context).size.width,
//height: 500,
child: Column(
children: [
//VideoPlayer(videoController!),
ValueListenableBuilder(
valueListenable: videoPlayerNotifier,
builder: (BuildContext cxt, play, widget_) {
return videoController.value.isInitialized
? AspectRatio(
aspectRatio: videoController.value.aspectRatio,
child: VideoPlayer(videoController))
: Container();
}),
const Text(
'movieName',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const Text('Description'),
FloatingActionButton(onPressed: () {
videoPlayerNotifier.value = true;
})
],
),
);
}
}
VideoPlayer from the video_player pub package is quite stateful, due especially to VideoPlayerController. However, you can wrap it in a StatefulWidget to be used within a StatelessWidget. E.g. use this wrapper within a stateless widget:
class VideoPlayerWrapper extends StatefulWidget {
final String videoUri;
const VideoPlayerWrapper({Key? key, required this.videoUri}) : super(key: key);
#override
_VideoPlayerWrapperState createState() => _VideoPlayerWrapperState(videoUri);
}
class _VideoPlayerWrapperState extends State<VideoPlayerWrapper> {
late VideoPlayerController _controller;
final String videoUri;
_VideoPlayerWrapperState(this.videoUri);
#override
void initState() {
super.initState();
_controller = VideoPlayerController.network(videoUri)
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
_controller.play();
}
#override
Widget build(BuildContext context) {
return Center(
child: _controller.value.isInitialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Container(),
);
}
#override
void dispose() {
super.dispose();
_controller.dispose();
}
}
I am getting below error while to implement YouTube video player in my flutter app.
Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: com.pichillilorenzo/flutter_inappwebview.
how to solve this issue?
YoutubePlayerController controller = YoutubePlayerController(initialVideoId: youtubId);
YoutubePlayer(
controller: controller,
showVideoProgressIndicator: false,
progressIndicatorColor: AppColors.primaryColor,
);
I am using this package
Is there any other better way for playing youtube videos in flutter (apart from Webview)?
import 'package:cooking_genie/src/presentation/core/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
class YoutubePlayerWidget extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return YoutubePlayerWidgetState();
}
}
class YoutubePlayerWidgetState extends State<YoutubePlayerWidget> {
//late YoutubePlayerController _controller;
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return YoutubePlayer(
controller: YoutubePlayerController(
initialVideoId: 'zn2GwbPG-tc', //Add videoID.
flags: YoutubePlayerFlags(
hideControls: false,
controlsVisibleAtStart: true,
autoPlay: false,
mute: false,
),
),
showVideoProgressIndicator: true,
progressIndicatorColor: AppColors.primaryColor,
);
}
#override
void dispose() {
super.dispose();
}
}
///
class Controls extends StatelessWidget {
///
const Controls();
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_space,
],
),
);
}
Widget get _space => const SizedBox(height: 10);
}
check this widget. and make sure to change minSdkVersion 22 on andorid>app>build.gradle. package require >=17 for android.
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
class YTplayerCheck extends StatefulWidget {
YTplayerCheck({Key? key}) : super(key: key);
#override
_YTplayerCheckState createState() => _YTplayerCheckState();
}
class _YTplayerCheckState extends State<YTplayerCheck> {
YoutubePlayerController controller = YoutubePlayerController(
initialVideoId: 'iLnmTe5Q2Qw',
flags: YoutubePlayerFlags(
autoPlay: true,
mute: true,
),
);
#override
Widget build(BuildContext context) {
return Scaffold(
body: YoutubePlayer(
controller: controller,
showVideoProgressIndicator: false,
progressIndicatorColor: Colors.cyanAccent,
),
);
}
}
I have managed to get a video to play on flutter-web, however, when I scroll the video in the ListView will rebuild/reload.
How can I stop the video_player (in the ListView) from being rebuilt when I scroll?
Sorry for the lengthy code sample.. I'm not sure how to condense it further
Any help would be great! Thanks
import 'package:flutter/material.dart';
import '../../widgets/video/chewie_video.dart';
import 'package:video_player/video_player.dart';
class TestPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Container(height: 1000),
Center(
child: ChewieVideo(
videoPlayerController:
VideoPlayerController.asset('assets/video.mp4'),
),
),
Container(height: 1000),
],
),
);
}
}
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class ChewieVideo extends StatefulWidget {
final VideoPlayerController videoPlayerController;
final bool looping;
ChewieVideo({
#required this.videoPlayerController,
this.looping,
Key key,
}) : super(key: key);
#override
_ChewieVideoState createState() => _ChewieVideoState();
}
class _ChewieVideoState extends State<ChewieVideo> {
ChewieController _chewieController;
#override
void initState() {
super.initState();
_chewieController = ChewieController(
videoPlayerController: widget.videoPlayerController,
autoInitialize: true,
aspectRatio: 16 / 9,
looping: widget.looping,
errorBuilder: (context, errorMessage) {
return Center(
child: Text(
errorMessage,
style: TextStyle(color: Colors.white),
),
);
},
);
}
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Chewie(
controller: _chewieController,
),
);
}
#override
void dispose() {
super.dispose();
widget.videoPlayerController.dispose();
_chewieController.dispose();
}
}
I'm not sure if this is a good or just temporary fix but this is how I have managed to get it to work...
If you are using inside of a ListView/CustomScrollView, then you need to adjust the cacheExtent for either of these widgets... I have set mine to 1000.
I'm not quite sure what's happening but it works...
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: primaryBlack,
drawer: NavDrawer(),
body: Scrollbar(
child: CustomScrollView(
cacheExtent: 1000,
slivers: <Widget>[
...
after doing that chewie and video_player plugins shouldn't rebuild/refresh inside of a ListView