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 all 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.3+2

* Update the homepage to point to the new plugin location

## 0.10.3+1

* Dispose `FLTVideoPlayer` in `onTextureUnregistered` callback on iOS.
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 <[email protected]>
version: 0.10.3+1
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player
version: 0.10.3+2
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,178 @@
// 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 'dart:ui';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.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(DataSource dataSource) async {
Map<String, dynamic> dataSourceDescription;

switch (dataSource.sourceType) {
case DataSourceType.asset:
dataSourceDescription = <String, dynamic>{
'asset': dataSource.asset,
'package': dataSource.package,
};
break;
case DataSourceType.network:
dataSourceDescription = <String, dynamic>{
'uri': dataSource.uri,
'formatHint': _videoFormatStringMap[dataSource.formatHint]
};
break;
case DataSourceType.file:
dataSourceDescription = <String, dynamic>{'uri': dataSource.uri};
break;
}

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, Duration position) {
return _channel.invokeMethod<void>(
'seekTo',
<String, dynamic>{
'textureId': textureId,
'location': position.inMilliseconds,
},
);
}

@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(
eventType: VideoEventType.initialized,
duration: Duration(milliseconds: map['duration']),
size: Size(map['width']?.toDouble() ?? 0.0,
map['height']?.toDouble() ?? 0.0),
);
case 'completed':
return VideoEvent(
eventType: VideoEventType.completed,
);
case 'bufferingUpdate':
final List<dynamic> values = map['values'];

return VideoEvent(
buffered: values.map<DurationRange>(_toDurationRange).toList(),
eventType: VideoEventType.completed,
);
case 'bufferingStart':
return VideoEvent(eventType: VideoEventType.bufferingStart);
case 'bufferingEnd':
return VideoEvent(eventType: VideoEventType.bufferingEnd);
default:
return VideoEvent(eventType: VideoEventType.unknown);
}
});
}

@override
Widget buildView(int textureId) {
return Texture(textureId: textureId);
}

EventChannel _eventChannelFor(int textureId) {
return EventChannel('flutter.io/videoPlayer/videoEvents$textureId');
}

static const Map<VideoFormat, String> _videoFormatStringMap =
<VideoFormat, String>{
VideoFormat.ss: 'ss',
VideoFormat.hls: 'hls',
VideoFormat.dash: 'dash',
VideoFormat.other: 'other',
};

DurationRange _toDurationRange(dynamic value) {
final List<dynamic> pair = value;
return DurationRange(
Duration(milliseconds: pair[0]),
Duration(milliseconds: pair[1]),
);
}
}
Loading