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

Commit 074afdb

Browse files
committed
Add video_player_platform_interface
1 parent 907efcb commit 074afdb

File tree

8 files changed

+503
-1
lines changed

8 files changed

+503
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
* Initial release.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# video_player_platform_interface
2+
3+
A common platform interface for the [`video_player`][1] plugin.
4+
5+
This interface allows platform-specific implementations of the `video_player`
6+
plugin, as well as the plugin itself, to ensure they are supporting the
7+
same interface.
8+
9+
# Usage
10+
11+
To implement a new platform-specific implementation of `video_player`, extend
12+
[`VideoPlayerPlatform`][2] with an implementation that performs the
13+
platform-specific behavior, and when you register your plugin, set the default
14+
`VideoPlayerPlatform` by calling
15+
`VideoPlayerPlatform.instance = MyPlatformVideoPlayer()`.
16+
17+
# Note on breaking changes
18+
19+
Strongly prefer non-breaking changes (such as adding a method to the interface)
20+
over breaking changes for this package.
21+
22+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
23+
on why a less-clean interface is preferable to a breaking change.
24+
25+
[1]: ../video_player
26+
[2]: lib/video_player_platform_interface.dart
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/services.dart';
8+
9+
import 'video_player_platform_interface.dart';
10+
11+
const MethodChannel _channel = MethodChannel('flutter.io/videoPlayer');
12+
13+
/// An implementation of [VideoPlayerPlatform] that uses method channels.
14+
class MethodChannelVideoPlayer extends VideoPlayerPlatform {
15+
@override
16+
Future<void> init() {
17+
return _channel.invokeMethod<void>('init');
18+
}
19+
20+
@override
21+
Future<void> dispose(int textureId) {
22+
return _channel.invokeMethod<void>(
23+
'dispose',
24+
<String, dynamic>{'textureId': textureId},
25+
);
26+
}
27+
28+
@override
29+
Future<int> create(Map<String, dynamic> dataSourceDescription) async {
30+
final Map<String, dynamic> response =
31+
await _channel.invokeMapMethod<String, dynamic>(
32+
'create',
33+
dataSourceDescription,
34+
);
35+
return response['textureId'];
36+
}
37+
38+
@override
39+
Future<void> setLooping(int textureId, bool looping) {
40+
return _channel.invokeMethod<void>(
41+
'setLooping',
42+
<String, dynamic>{
43+
'textureId': textureId,
44+
'looping': looping,
45+
},
46+
);
47+
}
48+
49+
@override
50+
Future<void> play(int textureId) {
51+
return _channel.invokeMethod<void>(
52+
'play',
53+
<String, dynamic>{'textureId': textureId},
54+
);
55+
}
56+
57+
@override
58+
Future<void> pause(int textureId) {
59+
return _channel.invokeMethod<void>(
60+
'pause',
61+
<String, dynamic>{'textureId': textureId},
62+
);
63+
}
64+
65+
@override
66+
Future<void> setVolume(int textureId, double volume) {
67+
return _channel.invokeMethod<void>(
68+
'setVolume',
69+
<String, dynamic>{
70+
'textureId': textureId,
71+
'volume': volume,
72+
},
73+
);
74+
}
75+
76+
@override
77+
Future<void> seekTo(int textureId, int milliseconds) {
78+
return _channel.invokeMethod<void>(
79+
'seekTo',
80+
<String, dynamic>{
81+
'textureId': textureId,
82+
'location': milliseconds,
83+
},
84+
);
85+
}
86+
87+
@override
88+
Future<Duration> getPosition(int textureId) async {
89+
return Duration(
90+
milliseconds: await _channel.invokeMethod<int>(
91+
'position',
92+
<String, dynamic>{'textureId': textureId},
93+
),
94+
);
95+
}
96+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:meta/meta.dart' show required, visibleForTesting;
8+
9+
import 'method_channel_video_player.dart';
10+
11+
/// The interface that implementations of video_player must implement.
12+
///
13+
/// Platform implementations should extend this class rather than implement it as `video_player`
14+
/// does not consider newly added methods to be breaking changes. Extending this class
15+
/// (using `extends`) ensures that the subclass will get the default implementation, while
16+
/// platform implementations that `implements` this interface will be broken by newly added
17+
/// [VideoPlayerPlatform] methods.
18+
abstract class VideoPlayerPlatform {
19+
/// Only mock implementations should set this to true.
20+
///
21+
/// Mockito mocks are implementing this class with `implements` which is forbidden for anything
22+
/// other than mocks (see class docs). This property provides a backdoor for mockito mocks to
23+
/// skip the verification that the class isn't implemented with `implements`.
24+
@visibleForTesting
25+
bool get isMock => false;
26+
27+
/// The default instance of [VideoPlayerPlatform] to use.
28+
///
29+
/// Platform-specific plugins should override this with their own
30+
/// platform-specific class that extends [VideoPlayerPlatform] when they
31+
/// register themselves.
32+
///
33+
/// Defaults to [MethodChannelVideoPlayer].
34+
static VideoPlayerPlatform _instance = MethodChannelVideoPlayer();
35+
36+
static VideoPlayerPlatform get instance => _instance;
37+
38+
// TODO(amirh): Extract common platform interface logic.
39+
// https://github.com/flutter/flutter/issues/43368
40+
static set instance(VideoPlayerPlatform instance) {
41+
if (!instance.isMock) {
42+
try {
43+
instance._verifyProvidesDefaultImplementations();
44+
} on NoSuchMethodError catch (_) {
45+
throw AssertionError(
46+
'Platform interfaces must not be implemented with `implements`');
47+
}
48+
}
49+
_instance = instance;
50+
}
51+
52+
/// Clears all open videos.
53+
Future<void> init() {
54+
throw UnimplementedError('init() has not been implemented.');
55+
}
56+
57+
/// Clears one video.
58+
Future<void> dispose(int textureId) {
59+
throw UnimplementedError('dispose() has not been implemented.');
60+
}
61+
62+
Future<int> create(
63+
Map<String, dynamic> dataSourceDescription,
64+
) {
65+
throw UnimplementedError('create() has not been implemented.');
66+
}
67+
68+
Future<void> setLooping(int textureId, bool looping) {
69+
throw UnimplementedError('setLooping() has not been implemented.');
70+
}
71+
72+
Future<void> play(int textureId) {
73+
throw UnimplementedError('play() has not been implemented.');
74+
}
75+
76+
Future<void> pause(int textureId) {
77+
throw UnimplementedError('pause() has not been implemented.');
78+
}
79+
80+
Future<void> setVolume(int textureId, double volume) {
81+
throw UnimplementedError('setVolume() has not been implemented.');
82+
}
83+
84+
Future<void> seekTo(int textureId, int milliseconds) {
85+
throw UnimplementedError('seekTo() has not been implemented.');
86+
}
87+
88+
Future<Duration> getPosition(int textureId) {
89+
throw UnimplementedError('getPosition() has not been implemented.');
90+
}
91+
92+
// This method makes sure that VideoPlayer isn't implemented with `implements`.
93+
//
94+
// See class doc for more details on why implementing this class is forbidden.
95+
//
96+
// This private method is called by the instance setter, which fails if the class is
97+
// implemented with `implements`.
98+
void _verifyProvidesDefaultImplementations() {}
99+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: video_player_platform_interface
2+
description: A common platform interface for the video_player plugin.
3+
author: Flutter Team <flutter-dev@googlegroups.com>
4+
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player_platform_interface
5+
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
6+
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7+
version: 1.0.0
8+
9+
dependencies:
10+
flutter:
11+
sdk: flutter
12+
meta: ^1.0.5
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
mockito: ^4.1.1
18+
19+
environment:
20+
sdk: ">=2.0.0-dev.28.0 <3.0.0"
21+
flutter: ">=1.9.1+hotfix.4 <2.0.0"

0 commit comments

Comments
 (0)