Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[video_player] Add platform interface #2273

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.4

* Add platform interface.

## 0.10.3

* Add support for the v2 Android embedding. This shouldn't impact existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

A Flutter plugin for iOS and Android for playing back video on a Widget surface.

![The example app running in iOS](https://github.com/flutter/plugins/blob/master/packages/video_player/doc/demo_ipod.gif?raw=true)
![The example app running in iOS](https://github.com/flutter/plugins/blob/master/packages/video_player/video_player/doc/demo_ipod.gif?raw=true)

*Note*: This plugin is still under development, and some APIs might not be available yet.
[Feedback welcome](https://github.com/flutter/flutter/issues) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: video_player
description: Flutter plugin for displaying inline video with other Flutter
widgets on Android and iOS.
author: Flutter Team <flutter-dev@googlegroups.com>
version: 0.10.3
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player
version: 0.10.4
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

* Initial release.
27 changes: 27 additions & 0 deletions packages/video_player/video_player_platform_interface/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 26 additions & 0 deletions packages/video_player/video_player_platform_interface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# video_player_platform_interface

A common platform interface for the [`video_player`][1] plugin.

This interface allows platform-specific implementations of the `video_player`
plugin, as well as the plugin itself, to ensure they are supporting the
same interface.

# Usage

To implement a new platform-specific implementation of `video_player`, extend
[`VideoPlayerPlatform`][2] with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`VideoPlayerPlatform` by calling
`VideoPlayerPlatform.instance = MyPlatformVideoPlayer()`.

# Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.

[1]: ../video_player
[2]: lib/video_player_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/services.dart';

import 'video_player_platform_interface.dart';

const MethodChannel _channel = MethodChannel('flutter.io/videoPlayer');

/// An implementation of [VideoPlayerPlatform] that uses method channels.
class MethodChannelVideoPlayer extends VideoPlayerPlatform {
@override
Future<void> init() {
return _channel.invokeMethod<void>('init');
}

@override
Future<void> dispose(int textureId) {
return _channel.invokeMethod<void>(
'dispose',
<String, dynamic>{'textureId': textureId},
);
}

@override
Future<int> create(Map<String, dynamic> dataSourceDescription) async {
final Map<String, dynamic> response =
await _channel.invokeMapMethod<String, dynamic>(
'create',
dataSourceDescription,
);
return response['textureId'];
}

@override
Future<void> setLooping(int textureId, bool looping) {
return _channel.invokeMethod<void>(
'setLooping',
<String, dynamic>{
'textureId': textureId,
'looping': looping,
},
);
}

@override
Future<void> play(int textureId) {
return _channel.invokeMethod<void>(
'play',
<String, dynamic>{'textureId': textureId},
);
}

@override
Future<void> pause(int textureId) {
return _channel.invokeMethod<void>(
'pause',
<String, dynamic>{'textureId': textureId},
);
}

@override
Future<void> setVolume(int textureId, double volume) {
return _channel.invokeMethod<void>(
'setVolume',
<String, dynamic>{
'textureId': textureId,
'volume': volume,
},
);
}

@override
Future<void> seekTo(int textureId, int milliseconds) {
return _channel.invokeMethod<void>(
'seekTo',
<String, dynamic>{
'textureId': textureId,
'location': milliseconds,
},
);
}

@override
Future<Duration> getPosition(int textureId) async {
return Duration(
milliseconds: await _channel.invokeMethod<int>(
'position',
<String, dynamic>{'textureId': textureId},
),
);
}

@override
Stream<VideoEvent> videoEventsFor(int textureId) {
return _eventChannelFor(textureId)
.receiveBroadcastStream()
.map((dynamic event) {
final Map<dynamic, dynamic> map = event;
switch (map['event']) {
case 'initialized':
return VideoEvent.initialized;
case 'completed':
return VideoEvent.completed;
case 'bufferingUpdate':
return VideoEvent.bufferingUpdate;
case 'bufferingStart':
return VideoEvent.bufferingStart;
case 'bufferingEnd':
return VideoEvent.bufferingEnd;
default:
return VideoEvent.unknown;
}
});
}

EventChannel _eventChannelFor(int textureId) {
return EventChannel('flutter.io/videoPlayer/videoEvents$textureId');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:meta/meta.dart' show visibleForTesting;

import 'method_channel_video_player.dart';

/// The interface that implementations of video_player must implement.
///
/// Platform implementations should extend this class rather than implement it as `video_player`
/// does not consider newly added methods to be breaking changes. Extending this class
/// (using `extends`) ensures that the subclass will get the default implementation, while
/// platform implementations that `implements` this interface will be broken by newly added
/// [VideoPlayerPlatform] methods.
abstract class VideoPlayerPlatform {
/// Only mock implementations should set this to true.
///
/// Mockito mocks are implementing this class with `implements` which is forbidden for anything
/// other than mocks (see class docs). This property provides a backdoor for mockito mocks to
/// skip the verification that the class isn't implemented with `implements`.
@visibleForTesting
bool get isMock => false;

/// The default instance of [VideoPlayerPlatform] to use.
///
/// Platform-specific plugins should override this with their own
/// platform-specific class that extends [VideoPlayerPlatform] when they
/// register themselves.
///
/// Defaults to [MethodChannelVideoPlayer].
static VideoPlayerPlatform _instance = MethodChannelVideoPlayer();

static VideoPlayerPlatform get instance => _instance;

// TODO(amirh): Extract common platform interface logic.
// https://github.com/flutter/flutter/issues/43368
static set instance(VideoPlayerPlatform instance) {
if (!instance.isMock) {
try {
instance._verifyProvidesDefaultImplementations();
} on NoSuchMethodError catch (_) {
throw AssertionError(
'Platform interfaces must not be implemented with `implements`');
}
}
_instance = instance;
}

/// Initializes the platform interface and disposes all existing players.
///
/// This method is called when the plugin is first initialized
/// and on every full restart.
Future<void> init() {
throw UnimplementedError('init() has not been implemented.');
}

/// Clears one video.
Future<void> dispose(int textureId) {
throw UnimplementedError('dispose() has not been implemented.');
}

/// Creates an instance of a video player.
///
/// For network and file sources [dataSourceDescription] needs to have a
/// [uri] key. Optionally you can set a [formatHint].
///
/// For assets the [asset] key is mandatory. Optionally you can specify
/// the [package].
/// TODO (cbenhagen): create a [DataSource] class
Future<int> create(
Map<String, dynamic> dataSourceDescription,
) {
throw UnimplementedError('create() has not been implemented.');
}

/// Returns a Stream of [VideoEvent]s.
Stream<VideoEvent> videoEventsFor(int textureId) {
throw UnimplementedError('videoEventsFor() has not been implemented.');
}

/// Sets the looping attribute of the video.
Future<void> setLooping(int textureId, bool looping) {
throw UnimplementedError('setLooping() has not been implemented.');
}

/// Starts the video playback.
Future<void> play(int textureId) {
throw UnimplementedError('play() has not been implemented.');
}

/// Stops the video playback.
Future<void> pause(int textureId) {
throw UnimplementedError('pause() has not been implemented.');
}

/// Sets the volume to a range between 0.0 and 1.0.
Future<void> setVolume(int textureId, double volume) {
throw UnimplementedError('setVolume() has not been implemented.');
}

/// Sets the video position to [milliseconds] from the start.
Future<void> seekTo(int textureId, int milliseconds) {
throw UnimplementedError('seekTo() has not been implemented.');
}

/// Gets the video position as [Duration] from the start.
Future<Duration> getPosition(int textureId) {
throw UnimplementedError('getPosition() has not been implemented.');
}

// This method makes sure that VideoPlayer isn't implemented with `implements`.
//
// See class doc for more details on why implementing this class is forbidden.
//
// This private method is called by the instance setter, which fails if the class is
// implemented with `implements`.
void _verifyProvidesDefaultImplementations() {}
}

enum VideoEvent {
initialized,
completed,
bufferingUpdate,
bufferingStart,
bufferingEnd,
unknown,
}
21 changes: 21 additions & 0 deletions packages/video_player/video_player_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: video_player_platform_interface
description: A common platform interface for the video_player plugin.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.0

dependencies:
flutter:
sdk: flutter
meta: ^1.0.5

dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.1

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
flutter: ">=1.9.1+hotfix.4 <2.0.0"
Loading