JustAudioPlugin.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #import "JustAudioPlugin.h"
  2. #import "AudioPlayer.h"
  3. #import <AVFoundation/AVFoundation.h>
  4. #include <TargetConditionals.h>
  5. @implementation JustAudioPlugin {
  6. NSObject<FlutterPluginRegistrar>* _registrar;
  7. BOOL _configuredSession;
  8. }
  9. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  10. FlutterMethodChannel* channel = [FlutterMethodChannel
  11. methodChannelWithName:@"com.ryanheise.just_audio.methods"
  12. binaryMessenger:[registrar messenger]];
  13. JustAudioPlugin* instance = [[JustAudioPlugin alloc] initWithRegistrar:registrar];
  14. [registrar addMethodCallDelegate:instance channel:channel];
  15. }
  16. - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
  17. self = [super init];
  18. NSAssert(self, @"super init cannot be nil");
  19. _registrar = registrar;
  20. return self;
  21. }
  22. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  23. if ([@"init" isEqualToString:call.method]) {
  24. NSArray* args = (NSArray*)call.arguments;
  25. NSString* playerId = args[0];
  26. /*AudioPlayer* player =*/ [[AudioPlayer alloc] initWithRegistrar:_registrar playerId:playerId configuredSession:_configuredSession];
  27. result(nil);
  28. } else if ([@"setIosCategory" isEqualToString:call.method]) {
  29. #if TARGET_OS_IPHONE
  30. NSNumber* categoryIndex = (NSNumber*)call.arguments;
  31. AVAudioSessionCategory category = nil;
  32. switch (categoryIndex.integerValue) {
  33. case 0: category = AVAudioSessionCategoryAmbient; break;
  34. case 1: category = AVAudioSessionCategorySoloAmbient; break;
  35. case 2: category = AVAudioSessionCategoryPlayback; break;
  36. case 3: category = AVAudioSessionCategoryRecord; break;
  37. case 4: category = AVAudioSessionCategoryPlayAndRecord; break;
  38. case 5: category = AVAudioSessionCategoryMultiRoute; break;
  39. }
  40. if (category) {
  41. _configuredSession = YES;
  42. }
  43. [[AVAudioSession sharedInstance] setCategory:category error:nil];
  44. #endif
  45. result(nil);
  46. } else {
  47. result(FlutterMethodNotImplemented);
  48. }
  49. }
  50. @end