ijk_event_channel.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'package:flutter/services.dart';
  2. import 'package:flutter_ijkplayer/src/video_info.dart';
  3. import './ijkplayer.dart';
  4. class IJKEventChannel {
  5. int get textureId => controller?.textureId;
  6. IjkMediaController controller;
  7. IJKEventChannel(this.controller);
  8. MethodChannel channel;
  9. String get channelName => "top.kikt/ijkplayer/event/$textureId";
  10. Future<void> init() async {
  11. channel = MethodChannel(channelName);
  12. channel.setMethodCallHandler(handler);
  13. }
  14. void dispose() {
  15. channel.setMethodCallHandler(null);
  16. controller = null;
  17. }
  18. Future<dynamic> handler(MethodCall call) async {
  19. switch (call.method) {
  20. case "finish": // 对应状态变化
  21. var index = call.arguments["type"];
  22. var type = FinishType.values[index];
  23. onPlayFinish(type);
  24. break;
  25. case "playStateChange":
  26. onPlayStateChange(getInfo(call));
  27. break;
  28. case "prepare":
  29. onPrepare(getInfo(call));
  30. break;
  31. default:
  32. return MissingPluginException(
  33. "$channelName ${call.method} not implement",
  34. );
  35. }
  36. }
  37. VideoInfo getInfo(MethodCall call) {
  38. var map = call.arguments.cast<String, dynamic>();
  39. return VideoInfo.fromMap(map);
  40. }
  41. void onPlayFinish(FinishType type) {
  42. print("onPlayFinish type = $type");
  43. }
  44. void onPlayStateChange(VideoInfo info) {
  45. print("onPlayStateChange $info");
  46. controller.isPlaying = info.isPlaying;
  47. }
  48. void onPrepare(VideoInfo info) {
  49. print("onPrepare $info");
  50. }
  51. }
  52. enum FinishType {
  53. playEnd,
  54. userExit,
  55. error,
  56. }