Created
March 6, 2020 07:19
-
-
Save haidar786/42ded58f9ad2244c5117b916703d442a to your computer and use it in GitHub Desktop.
This file contains 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
import 'package:flutter/material.dart'; | |
import 'package:video_player/video_player.dart'; | |
import 'package:youtube_explode_dart/youtube_explode_dart.dart' as yt; | |
class VideoPlayerPage extends StatelessWidget { | |
VideoPlayerPage({this.videoId, this.videoName}); | |
final String videoId; | |
final String videoName; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
// appBar: AppBar(title: Text(videoName),), | |
body: FutureBuilder<yt.MediaStreamInfoSet>( | |
future: yt.YoutubeExplode().getVideoMediaStream(videoId), | |
builder: (context, snapshot) { | |
yt.YoutubeExplode().close(); | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
return Center( | |
child: Text("none"), | |
); | |
break; | |
case ConnectionState.waiting: | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
break; | |
case ConnectionState.active: | |
return Center( | |
child: Text("active"), | |
); | |
break; | |
case ConnectionState.done: | |
if (snapshot.hasData) { | |
print(snapshot.data.video.first.url); | |
return VideoPlayerWidget( | |
snapshot.data.audio.last.url.toString()); | |
} else { | |
return Center( | |
child: Text(snapshot.error.toString()), | |
); | |
} | |
break; | |
} | |
return Center( | |
child: Text("Uh oh! Something went wrong"), | |
); | |
}, | |
), | |
); | |
} | |
} | |
class VideoPlayerWidget extends StatefulWidget { | |
VideoPlayerWidget(this.url); | |
final String url; | |
@override | |
_VideoPlayerWidgetState createState() => _VideoPlayerWidgetState(); | |
} | |
class _VideoPlayerWidgetState extends State<VideoPlayerWidget> { | |
VideoPlayerController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = VideoPlayerController.network(widget.url) | |
..initialize().then((_) { | |
setState(() { | |
_controller.play(); | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: _controller.value.initialized | |
? AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
child: VideoPlayer(_controller), | |
) | |
: Container(), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
_controller.value.isPlaying | |
? _controller.pause() | |
: _controller.play(); | |
}); | |
}, | |
child: Icon( | |
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow, | |
), | |
), | |
); | |
} | |
@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