ijk_event_channel.dart 2.0 KB

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