video_launcher 0.3.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 42

VideoLauncher

Example

cd flutter_video_launcher/example
flutter packages get
flutter run

screenshot

Usage

To use this plugin, add video_launcher as a dependency in your pubspec.yaml file.

After importing 'package:video_launcher/video_launcher.dart' the directories can be queried as follows

Remote video

To launch a video :

Future<Null> _launch(String url) async {
    if (await canLaunchVideo(url)) {
      await launchVideo(url);
    } else {
      throw 'Could not launch $url';
    }
  }

Local video

To play a local video file :

Future<Null> _launch(String url) async {
    if (await canLaunchVideo(url, isLocal:true)) {
      await launchVideo(url, isLocal:true);
    } else {
      throw 'Could not launch $url';
    }
  }

Embedded asset

To play video embedded assets, you can copy them to the app storage folder, and the play them "locally"

void _loadOrLaunchLocalAsset() =>
      localAssetPath != null ? _launchLocalAsset() : copyLocalAsset();

Future copyLocalAsset() async {
  final dir = await getApplicationDocumentsDirectory();
  final file = new File("${dir.path}/video_asset.mp4");
  final videoData = await rootBundle.load("assets/video.mp4");
  final bytes = videoData.buffer.asUint8List();
  file.writeAsBytes(bytes, flush: true);
    setState(() {
    localAssetPath = file.path;
    _launchLocalAsset();
});
}

void _launchLocalAsset() =>
  setState(() => _launched = _launchVideo(localAssetPath, isLocal: true));

Please see the example app of this plugin for a full example.

⚠️ iOS App Transport Security

By default iOS forbids loading from non-https url. To cancel this restriction edit your .plist and add :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

cf. Configuring App Transport Security Exceptions in iOS

Troubleshooting

If you get a MissingPluginException, try to flutter build apk on Android, or flutter build ios

Getting Started

For help getting started with Flutter, view our online documentation.

Changelog

0.3.0

  • fix the local video playing on android
  • fix the example

0.2.2

  • changed Flutter dependency in pubspec.yaml to an SDK dependency

0.2.1

  • fix the iOS local playing
  • add download/play local in the example

0.2.0

  • android

0.1.0

  • iOS

example/lib/main.dart

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.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;
  String localAssetPath;

  _buildButton(String label, VoidCallback onPressed) => new Padding(
      padding: new EdgeInsets.all(12.0),
      child: new RaisedButton(
        onPressed: onPressed,
        child: new Text(label),
      ));

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

  @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),
                ),
                _buildButton('Play online', _launch),
                _buildButton(
                    'Download', localVideoPath != null ? null : _loadVideo),
                _buildButton(
                    'Play local', localVideoPath != null ? _launchLocal : null),
                _buildButton('Play video asset', _loadOrLaunchLocalAsset),
              ],
            ),
            new FutureBuilder<Null>(future: _launched, builder: _launchStatus),
          ],
        ),
      ),
    );
  }

  void _launch() => setState(() => _launched = _launchVideo(videoUrl));

  void _launchLocal() =>
      setState(() => _launched = _launchVideo(localVideoPath, isLocal: true));

  void _loadOrLaunchLocalAsset() =>
      localAssetPath != null ? _launchLocalAsset() : copyLocalAsset();

  Future copyLocalAsset() async {
    final dir = await getApplicationDocumentsDirectory();
    final file = new File("${dir.path}/video_asset.mp4");
    final videoData = await rootBundle.load("assets/video.mp4");
    final bytes = videoData.buffer.asUint8List();
    await file.writeAsBytes(bytes, flush: true);
    setState(() {
      localAssetPath = file.path;
      _launchLocalAsset();
    });
  }

  void _launchLocalAsset() =>
      _launched = _launchVideo(localAssetPath, 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;
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  video_launcher: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:video_launcher/video_launcher.dart';
  
Version Uploaded Documentation Archive
0.3.0 Sep 6, 2017 Go to the documentation of video_launcher 0.3.0 Download video_launcher 0.3.0 archive
0.2.2 May 25, 2017 Go to the documentation of video_launcher 0.2.2 Download video_launcher 0.2.2 archive
0.2.1 May 16, 2017 Go to the documentation of video_launcher 0.2.1 Download video_launcher 0.2.1 archive
0.2.0+1 May 15, 2017 Go to the documentation of video_launcher 0.2.0+1 Download video_launcher 0.2.0+1 archive
0.2.0 May 15, 2017 Go to the documentation of video_launcher 0.2.0 Download video_launcher 0.2.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
85
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
42
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0