PSDK-989 Simply init android methods

This commit is contained in:
Николай Коротков
2023-09-22 16:26:23 +03:00
committed by Elena Nazarova
parent e12f6886bf
commit 4432a4326c
3 changed files with 146 additions and 36 deletions
@@ -1,17 +1,122 @@
package tech.nut.nut_player_android
import android.content.Context
import android.view.View
import io.flutter.FlutterInjector
import io.flutter.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.PluginRegistry
import io.flutter.view.TextureRegistry
/** NutPlayerAndroidPlugin */
/** Android platform implementation of the VideoPlayerPlugin. */
class NutPlayerAndroidPlugin : FlutterPlugin {
private val videoPlayers = emptyMap<Int, View>().toMutableMap()
private var flutterState: FlutterState? = null
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
binding.platformViewRegistry.registerViewFactory(
"nut_player_android_view", NutPlayerAndroidViewFactory(binding.binaryMessenger)
)
}
/** Register this with the v2 embedding for the plugin to respond to lifecycle callbacks. */
constructor()
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
TODO("Not yet implemented")
}
@Suppress("deprecation")
private constructor(registrar: PluginRegistry.Registrar) {
flutterState = FlutterState(
registrar.context(),
registrar.messenger(),
registrar.textures()
)
flutterState?.startListening(this, registrar.messenger())
}
private val TAG = "VideoPlayerPlugin"
/** Registers this with the stable v1 embedding. Will not respond to lifecycle events. */
@Suppress("deprecation")
fun registerWith(registrar: PluginRegistry.Registrar) {
val plugin = NutPlayerAndroidPlugin(registrar)
registrar.addViewDestroyListener {
plugin.onDestroy()
false // We are not interested in assuming ownership of the NativeView.
}
}
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
val injector = FlutterInjector.instance()
flutterState = FlutterState(
binding.applicationContext,
binding.binaryMessenger,
binding.textureRegistry
)
flutterState?.startListening(this, binding.binaryMessenger)
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
if (flutterState == null) {
Log.wtf(TAG, "Detached from the engine before registering to it.")
}
flutterState?.stopListening(binding.binaryMessenger)
flutterState = null
initialize()
}
private fun disposeAllPlayers() {
videoPlayers.clear()
}
private fun onDestroy() {
// The whole FlutterView is being destroyed. Here we release resources acquired for all
// instances
// of VideoPlayer. Once https://github.com/flutter/flutter/issues/19358 is resolved this may
// be replaced with just asserting that videoPlayers.isEmpty().
// https://github.com/flutter/flutter/issues/20989 tracks this.
disposeAllPlayers()
}
fun initialize() {
disposeAllPlayers()
}
fun create(): Int {
val handle: TextureRegistry.SurfaceTextureEntry? =
flutterState?.textureRegistry?.createSurfaceTexture()
val player = View(
flutterState?.applicationContext
)
player.setBackgroundColor(0x0000FF00)
return if (handle != null) {
val id = handle.id().toInt()
videoPlayers[id] = player
id
} else {
-1
}
}
fun dispose(id: Int) {
videoPlayers.remove(id)
}
//TODO() - В FlutterState возможно есть ошибка
private class FlutterState constructor(
val applicationContext: Context,
val binaryMessenger: BinaryMessenger,
textureRegistry: TextureRegistry
) {
val textureRegistry: TextureRegistry
init {
this.textureRegistry = textureRegistry
}
//TODO() - разобраться с этими слушателями
fun startListening(
methodCallHandler: NutPlayerAndroidPlugin?,
messenger: BinaryMessenger?
) {
AndroidVideoPlayerApi.setup(messenger, methodCallHandler)
}
fun stopListening(messenger: BinaryMessenger?) {
AndroidVideoPlayerApi.setup(messenger, null)
}
}
}
+29 -27
View File
@@ -1,38 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:nut_player_platform_interface/nut_player_platform_interface.dart';
typedef NutPlayerAndroidViewCreatedCallback = void Function(
NutPlayerAndroidViewController controller);
/// An Android implementation of [NutPlayerPlatform]
class NutPlayerAndroidPlatform extends NutPlayerPlatform {
//TODO - разобраться с каналами
late MethodChannel _channel;
class NutPlayerAndroid extends StatelessWidget {
final NutPlayerAndroidViewCreatedCallback onMapViewCreated;
const NutPlayerAndroid({Key? key, required this.onMapViewCreated})
: super(key: key);
/// Registers this class as the default instance of [PathProviderPlatform].
static void registerWith() {
NutPlayerPlatform.instance = NutPlayerAndroidPlatform();
}
@override
Widget build(BuildContext context) {
return AndroidView(
viewType: 'nut_player_android_view',
onPlatformViewCreated: _onPlatformViewCreated,
);
Future<void> init() {
return _channel.invokeMethod('initialize');
}
void _onPlatformViewCreated(int id) =>
onMapViewCreated(NutPlayerAndroidViewController._(id));
}
class NutPlayerAndroidViewController {
NutPlayerAndroidViewController._(int id)
: _channel = MethodChannel('nut_player_android_view_$id');
final MethodChannel _channel;
Future<void> setUrl({required String url}) async {
return _channel.invokeMethod('setUrl', url);
@override
Future<void> dispose(PlayerId id) {
return _channel.invokeMethod('dispose', {'textureId': id});
}
Future<void> play() async {
_channel.invokeMethod('play');
@override
Future<PlayerId> create(DataSource dataSource) async {
final response = _channel.invokeMethod<PlayerId>("create");
var playerId = await Future.wait([response]);
if (playerId.first != null) {
return Future<PlayerId>.value(playerId.first!);
} else {
throw("Fail on player creation");
}
}
@override
Widget buildView(PlayerId playerId) {
return Texture(textureId: playerId);
}
}
+3
View File
@@ -11,6 +11,8 @@ dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2
nut_player_platform_interface:
path: ../nut_player_platform_interface
dev_dependencies:
flutter_test:
@@ -36,6 +38,7 @@ flutter:
platforms:
android:
package: tech.nut.nut_player_android
dartPluginClass: NutPlayerAndroidPlatform
pluginClass: NutPlayerAndroidPlugin
# To add assets to your plugin package, add an assets section, like this: