video_launcher 0.2.1 video_launcher: ^0.2.1 copied to clipboard
Fullscreen video launcher plugin
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_launcher/video_launcher.dart';
void main() {
runApp(new MyApp());
}
typedef void OnError(Exception exception);
const videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
/// a video launcher, based on url_launcher
/// can read remote and local files
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Video Launcher',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Video Launcher'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<Null> _launched;
String localVideoPath;
_buildButton(String label, VoidCallback onPressed) => new Padding( padding: new EdgeInsets.all(12.0), child: new RaisedButton(
onPressed: onPressed,
child: new Text(label),
) );
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Column(
mainAxisSize: MainAxisSize.min,
children: [
new Padding(
padding: new EdgeInsets.all(16.0),
child: new Text(videoUrl),
),
/*new Row(mainAxisSize: MainAxisSize.min, children: [*/
_buildButton('Play online', _launch),
_buildButton('Download', localVideoPath != null ? null : _loadVideo),
_buildButton('Play local', localVideoPath != null ? _launchLocal : null),
/*]),*/
],
),
new FutureBuilder<Null>(future: _launched, builder: _launchStatus),
],
),
),
);
}
void _launch() {
setState(() {
_launched = _launchVideo(videoUrl);
});
}
void _launchLocal() {
setState(() {
_launched = _launchVideo(localVideoPath, isLocal: true);
});
}
Widget _launchStatus(BuildContext context, AsyncSnapshot<Null> snapshot) {
String info ;
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
} else {
info = localVideoPath != null ? localVideoPath : '';
}
return new Padding( padding: new EdgeInsets.only(top: 20.0), child: new Text(info) );
}
Future<Null> _launchVideo(String url, {bool isLocal: false}) async {
if (await canLaunchVideo(url, isLocal: isLocal)) {
await launchVideo(url, isLocal: isLocal);
} else {
throw 'Could not launch $url';
}
}
Future _loadVideo() async {
final bytes = await _loadFileBytes(videoUrl,
onError: (Exception exception) =>
print('_MyHomePageState._loadVideo => exception ${exception}'));
final dir = await getApplicationDocumentsDirectory();
final file = new File('${dir.path}/video.mp4');
await file.writeAsBytes(bytes);
if (await file.exists())
setState(() {
localVideoPath = file.path;
});
}
Future<Uint8List> _loadFileBytes(String url, {OnError onError}) async {
Uint8List bytes;
try {
bytes = await readBytes(url);
} on ClientException {
rethrow;
}
return bytes;
}
}