Created
June 22, 2021 07:24
-
-
Save maheshj01/d7eeaa5b7fbc5e713fcbd7efee55054d to your computer and use it in GitHub Desktop.
The video feels laggy when trying to play multiple video from network and while 1st video is playing when tried to play second video the first video seems to be buffering.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
runApp(HomePage()); | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({Key? key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: ListView.builder( | |
itemCount: 20, itemBuilder: (_, x) => VideoWidget()), | |
), | |
); | |
} | |
} | |
class VideoWidget extends StatefulWidget { | |
@override | |
_VideoWidgetState createState() => _VideoWidgetState(); | |
} | |
class _VideoWidgetState extends State<VideoWidget> { | |
late VideoPlayerController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = VideoPlayerController.network( | |
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4', | |
)..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 Stack( | |
alignment: Alignment.center, | |
children: [ | |
Container( | |
height: 250, | |
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 6), | |
decoration: BoxDecoration(), | |
child: _controller.value.isInitialized | |
? AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
child: VideoPlayer(_controller), | |
) | |
: Container( | |
color: Colors.grey, | |
), | |
), | |
GestureDetector( | |
onTap: () async { | |
final duration = await _controller.position; | |
int inSeconds = duration!.inSeconds; | |
if (inSeconds == _controller.value.duration.inSeconds) { | |
/// restart the video | |
await _controller.seekTo(Duration.zero); | |
} else { | |
_controller.value.isPlaying | |
? await _controller.pause() | |
: await _controller.play(); | |
} | |
setState(() {}); | |
}, | |
child: Center( | |
child: !_controller.value.isPlaying | |
? Icon( | |
Icons.play_arrow, | |
color: Colors.white, | |
) | |
: Icon( | |
Icons.pause, | |
color: Colors.white, | |
)), | |
) | |
], | |
); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
_controller.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment