FlutterIJK.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Created by Caijinglong on 2019-03-08.
  3. //
  4. #import "FlutterIJK.h"
  5. #import <IJKMediaFramework/IJKMediaFramework.h>
  6. #import <IJKMediaFramework/IJKMediaPlayer.h>
  7. #import <AVFoundation/AVFoundation.h>
  8. @interface IJKVideoPlayer : NSObject <FlutterTexture>
  9. @property(nonatomic, strong) IJKFFMoviePlayerController *controller;
  10. - (void)setDataSource:(NSString *)uri;
  11. @end
  12. @interface FlutterIJK ()
  13. @end
  14. @implementation FlutterIJK {
  15. int64_t textureId;
  16. IJKVideoPlayer *player;
  17. }
  18. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  19. self = [super init];
  20. if (self) {
  21. self.registrar = registrar;
  22. IJKMPMoviePlayerController *controller = [[IJKMPMoviePlayerController alloc] initWithContentURLString:@""];
  23. NSObject <FlutterTextureRegistry> *textures = [self.registrar textures];
  24. player = [IJKVideoPlayer new];
  25. textureId = [textures registerTexture:player];
  26. }
  27. return self;
  28. }
  29. + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  30. return [[self alloc] initWithRegistrar:registrar];
  31. }
  32. - (int64_t)id {
  33. return textureId;
  34. }
  35. - (void)dispose {
  36. IJKFFMoviePlayerController *ctl = [player controller];
  37. [ctl stop];
  38. [ctl shutdown];
  39. }
  40. - (void)play {
  41. IJKFFMoviePlayerController *ctl = [player controller];
  42. [ctl play];
  43. }
  44. - (void)pause {
  45. [[player controller] pause];
  46. }
  47. - (void)stop {
  48. [[player controller] stop];
  49. }
  50. - (void)setDateSourceWithUri:(NSString *)uri {
  51. [player setDataSource:uri];
  52. }
  53. @end
  54. @implementation IJKVideoPlayer {
  55. // IJKFFMoviePlayerController *controller;
  56. }
  57. - (instancetype)init {
  58. self = [super init];
  59. if (self) {
  60. }
  61. return self;
  62. }
  63. - (CVPixelBufferRef _Nullable)copyPixelBuffer {
  64. return [self.controller framePixelbuffer];
  65. }
  66. - (void)setDataSource:(NSString *)uri {
  67. IJKFFOptions *options = [IJKFFOptions optionsByDefault];
  68. self.controller = [[IJKFFMoviePlayerController alloc] initWithContentURLString:uri withOptions:options];
  69. [self.controller prepareToPlay];
  70. }
  71. @end