CoolIjkNotifyChannel.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Created by Caijinglong on 2019-03-15.
  3. //
  4. #import "CoolIjkNotifyChannel.h"
  5. #import "CoolVideoInfo.h"
  6. @implementation CoolIjkNotifyChannel {
  7. FlutterMethodChannel *channel;
  8. }
  9. - (instancetype)initWithController:(IJKFFMoviePlayerController *)controller textureId:(int64_t)textureId
  10. registrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  11. self = [super init];
  12. if (self) {
  13. self.controller = controller;
  14. self.textureId = textureId;
  15. self.registrar = registrar;
  16. [self initial];
  17. }
  18. return self;
  19. }
  20. + (instancetype)channelWithController:(IJKFFMoviePlayerController *)controller textureId:(int64_t)textureId
  21. registrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  22. return [[self alloc] initWithController:controller textureId:textureId registrar:registrar];
  23. }
  24. - (void)initial {
  25. NSString *channelName = [NSString stringWithFormat:@"top.kikt/ijkplayer/event/%lli", self.textureId];
  26. channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:[self.registrar messenger]];
  27. [self registerObserver];
  28. }
  29. - (void)dispose {
  30. channel = nil;
  31. [self unregisterObservers];
  32. }
  33. - (void)registerObserver {
  34. [[NSNotificationCenter defaultCenter] addObserver:self
  35. selector:@selector(loadStateDidChange:)
  36. name:IJKMPMoviePlayerLoadStateDidChangeNotification
  37. object:_controller];
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:@selector(moviePlayBackFinish:)
  40. name:IJKMPMoviePlayerPlaybackDidFinishNotification
  41. object:_controller];
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(mediaIsPreparedToPlayDidChange:)
  44. name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
  45. object:_controller];
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(moviePlayBackStateDidChange:)
  48. name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
  49. object:_controller];
  50. [[NSNotificationCenter defaultCenter] addObserver:self
  51. selector:@selector(movieRotationChange:)
  52. name:IJKMPMoviePlayerVideoRotationNotification
  53. object:_controller];
  54. }
  55. - (void)unregisterObservers {
  56. [[NSNotificationCenter defaultCenter] removeObserver:self
  57. name:IJKMPMoviePlayerLoadStateDidChangeNotification
  58. object:_controller];
  59. [[NSNotificationCenter defaultCenter] removeObserver:self
  60. name:IJKMPMoviePlayerPlaybackDidFinishNotification
  61. object:_controller];
  62. [[NSNotificationCenter defaultCenter] removeObserver:self
  63. name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
  64. object:_controller];
  65. [[NSNotificationCenter defaultCenter] removeObserver:self
  66. name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
  67. object:_controller];
  68. [[NSNotificationCenter defaultCenter] removeObserver:self
  69. name:IJKMPMoviePlayerVideoRotationNotification
  70. object:_controller];
  71. }
  72. - (NSDictionary *)getInfo {
  73. return [[_infoDelegate getInfo] toMap];
  74. }
  75. - (void)moviePlayBackStateDidChange:(NSNotification *)notification {
  76. [channel invokeMethod:@"playStateChange" arguments:[self getInfo]];
  77. }
  78. - (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification {
  79. [channel invokeMethod:@"prepare" arguments:[self getInfo]];
  80. }
  81. - (void)moviePlayBackFinish:(NSNotification *)notification {
  82. int reason = [[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
  83. int type = 2;
  84. switch (reason) {
  85. case IJKMPMovieFinishReasonPlaybackEnded:
  86. type = 0;
  87. break;
  88. case IJKMPMovieFinishReasonUserExited:
  89. type = 1;
  90. break;
  91. case IJKMPMovieFinishReasonPlaybackError:{
  92. int errValue = [[[notification userInfo] valueForKey:@"error"] intValue];
  93. [channel invokeMethod:@"error" arguments:@(errValue)];
  94. return;
  95. }
  96. default:
  97. break;
  98. }
  99. NSLog(@"type = %d", type);
  100. // [channel invokeMethod:@"finish" arguments:@{@"type": @(type)}];
  101. [channel invokeMethod:@"finish" arguments:[self getInfo]];
  102. }
  103. - (void)loadStateDidChange:(NSNotification *)notification {
  104. NSLog(@"load state change, state = %lu", (unsigned long)_controller.loadState);
  105. [self.infoDelegate onLoadStateChange];
  106. }
  107. - (void)movieRotationChange:(NSNotification *)notification {
  108. int rotate = [[[notification userInfo] valueForKey:IJKMPMoviePlayerVideoRotationRotateUserInfoKey] intValue];
  109. [_infoDelegate setDegree:rotate];
  110. [channel invokeMethod:@"rotateChanged" arguments:[self getInfo]];
  111. }
  112. @end