ijk_event_channel.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return true;
  39. }
  40. VideoInfo getInfo(MethodCall call) {
  41. var map = call.arguments.cast<String, dynamic>();
  42. return VideoInfo.fromMap(map);
  43. }
  44. void onPlayFinish(VideoInfo info) {
  45. controller.isPlaying = info.isPlaying;
  46. }
  47. void onPlayStateChange(VideoInfo info) {
  48. controller.isPlaying = info.isPlaying;
  49. }
  50. void onPrepare(VideoInfo info) {
  51. controller.isPlaying = info.isPlaying;
  52. _prepareCompleter?.complete();
  53. _prepareCompleter = null;
  54. }
  55. Future<void> waitPrepare() {
  56. _prepareCompleter = Completer();
  57. return _prepareCompleter.future;
  58. }
  59. void autoPlay(IjkMediaController ijkMediaController) async {
  60. try {
  61. await waitPrepare();
  62. ijkMediaController.play();
  63. } catch (e) {
  64. print(e);
  65. }
  66. }
  67. }
  68. // enum FinishType {
  69. // playEnd,
  70. // userExit,
  71. // error,
  72. // }