FlutterSoundPlugin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #import "FlutterSoundPlugin.h"
  2. #import <AVFoundation/AVFoundation.h>
  3. // post fix with _FlutterSound to avoid conflicts with common libs including path_provider
  4. NSString* GetDirectoryOfType_FlutterSound(NSSearchPathDirectory dir) {
  5. NSArray* paths = NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES);
  6. return [paths.firstObject stringByAppendingString:@"/"];
  7. }
  8. @implementation FlutterSoundPlugin{
  9. NSURL *audioFileURL;
  10. AVAudioRecorder *audioRecorder;
  11. AVAudioPlayer *audioPlayer;
  12. NSTimer *timer;
  13. NSTimer *dbPeakTimer;
  14. }
  15. double subscriptionDuration = 0.01;
  16. double dbPeakInterval = 0.8;
  17. bool shouldProcessDbLevel = false;
  18. FlutterMethodChannel* _channel;
  19. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
  20. NSLog(@"audioPlayerDidFinishPlaying");
  21. NSNumber *duration = [NSNumber numberWithDouble:audioPlayer.duration * 1000];
  22. NSNumber *currentTime = [NSNumber numberWithDouble:audioPlayer.currentTime * 1000];
  23. NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
  24. /*
  25. NSDictionary *status = @{
  26. @"duration" : [duration stringValue],
  27. @"current_position" : [currentTime stringValue],
  28. };
  29. */
  30. [_channel invokeMethod:@"audioPlayerDidFinishPlaying" arguments:status];
  31. [self stopTimer];
  32. }
  33. - (void) stopTimer{
  34. if (timer != nil) {
  35. [timer invalidate];
  36. timer = nil;
  37. }
  38. }
  39. - (void)updateRecorderProgress:(NSTimer*) timer
  40. {
  41. NSNumber *currentTime = [NSNumber numberWithDouble:audioRecorder.currentTime * 1000];
  42. [audioRecorder updateMeters];
  43. NSString* status = [NSString stringWithFormat:@"{\"current_position\": \"%@\"}", [currentTime stringValue]];
  44. /*
  45. NSDictionary *status = @{
  46. @"current_position" : [currentTime stringValue],
  47. };
  48. */
  49. [_channel invokeMethod:@"updateRecorderProgress" arguments:status];
  50. }
  51. - (void)updateProgress:(NSTimer*) timer
  52. {
  53. NSNumber *duration = [NSNumber numberWithDouble:audioPlayer.duration * 1000];
  54. NSNumber *currentTime = [NSNumber numberWithDouble:audioPlayer.currentTime * 1000];
  55. if ([duration intValue] == 0 && timer != nil) {
  56. [self stopTimer];
  57. return;
  58. }
  59. NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
  60. /*
  61. NSDictionary *status = @{
  62. @"duration" : [duration stringValue],
  63. @"current_position" : [currentTime stringValue],
  64. };
  65. */
  66. [_channel invokeMethod:@"updateProgress" arguments:status];
  67. }
  68. - (void)updateDbPeakProgress:(NSTimer*) dbPeakTimer
  69. {
  70. NSNumber *normalizedPeakLevel = [NSNumber numberWithDouble:MIN(pow(10.0, [audioRecorder peakPowerForChannel:0] / 20.0) * 160.0, 160.0)];
  71. [_channel invokeMethod:@"updateDbPeakProgress" arguments:normalizedPeakLevel];
  72. }
  73. - (void)startRecorderTimer
  74. {
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. self->timer = [NSTimer scheduledTimerWithTimeInterval: subscriptionDuration
  77. target:self
  78. selector:@selector(updateRecorderProgress:)
  79. userInfo:nil
  80. repeats:YES];
  81. });
  82. }
  83. - (void)startTimer
  84. {
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. self->timer = [NSTimer scheduledTimerWithTimeInterval:subscriptionDuration
  87. target:self
  88. selector:@selector(updateProgress:)
  89. userInfo:nil
  90. repeats:YES];
  91. });
  92. }
  93. - (void)startDbTimer
  94. {
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. self->dbPeakTimer = [NSTimer scheduledTimerWithTimeInterval:dbPeakInterval
  97. target:self
  98. selector:@selector(updateDbPeakProgress:)
  99. userInfo:nil
  100. repeats:YES];
  101. });
  102. }
  103. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  104. FlutterMethodChannel* channel = [FlutterMethodChannel
  105. methodChannelWithName:@"flutter_sound"
  106. binaryMessenger:[registrar messenger]];
  107. FlutterSoundPlugin* instance = [[FlutterSoundPlugin alloc] init];
  108. [registrar addMethodCallDelegate:instance channel:channel];
  109. _channel = channel;
  110. }
  111. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  112. if ([@"startRecorder" isEqualToString:call.method]) {
  113. NSString* path = (NSString*)call.arguments[@"path"];
  114. NSNumber* sampleRateArgs = (NSNumber*)call.arguments[@"sampleRate"];
  115. NSNumber* numChannelsArgs = (NSNumber*)call.arguments[@"numChannels"];
  116. NSNumber* iosQuality = (NSNumber*)call.arguments[@"iosQuality"];
  117. NSNumber* bitRate = (NSNumber*)call.arguments[@"bitRate"];
  118. float sampleRate = 44100;
  119. if (![sampleRateArgs isKindOfClass:[NSNull class]]) {
  120. sampleRate = [sampleRateArgs integerValue];
  121. }
  122. int numChannels = 2;
  123. if (![numChannelsArgs isKindOfClass:[NSNull class]]) {
  124. numChannels = [numChannelsArgs integerValue];
  125. }
  126. [self startRecorder:path:[NSNumber numberWithInt:numChannels]:[NSNumber numberWithInt:sampleRate]:iosQuality:bitRate result:result];
  127. } else if ([@"stopRecorder" isEqualToString:call.method]) {
  128. [self stopRecorder:result];
  129. } else if ([@"startPlayer" isEqualToString:call.method]) {
  130. NSString* path = (NSString*)call.arguments[@"path"];
  131. [self startPlayer:path result:result];
  132. } else if ([@"stopPlayer" isEqualToString:call.method]) {
  133. [self stopPlayer:result];
  134. } else if ([@"pausePlayer" isEqualToString:call.method]) {
  135. [self pausePlayer:result];
  136. } else if ([@"resumePlayer" isEqualToString:call.method]) {
  137. [self resumePlayer:result];
  138. } else if ([@"seekToPlayer" isEqualToString:call.method]) {
  139. NSNumber* sec = (NSNumber*)call.arguments[@"sec"];
  140. [self seekToPlayer:sec result:result];
  141. } else if ([@"setSubscriptionDuration" isEqualToString:call.method]) {
  142. NSNumber* sec = (NSNumber*)call.arguments[@"sec"];
  143. [self setSubscriptionDuration:[sec doubleValue] result:result];
  144. } else if ([@"setVolume" isEqualToString:call.method]) {
  145. NSNumber* volume = (NSNumber*)call.arguments[@"volume"];
  146. [self setVolume:[volume doubleValue] result:result];
  147. }
  148. else if ([@"setDbPeakLevelUpdate" isEqualToString:call.method]) {
  149. NSNumber* intervalInSecs = (NSNumber*)call.arguments[@"intervalInSecs"];
  150. [self setDbPeakLevelUpdate:[intervalInSecs doubleValue] result:result];
  151. }
  152. else if ([@"setDbLevelEnabled" isEqualToString:call.method]) {
  153. BOOL enabled = [call.arguments[@"enabled"] boolValue];
  154. [self setDbLevelEnabled:enabled result:result];
  155. }
  156. else {
  157. result(FlutterMethodNotImplemented);
  158. }
  159. }
  160. - (void)setSubscriptionDuration:(double)duration result: (FlutterResult)result {
  161. subscriptionDuration = duration;
  162. result(@"setSubscriptionDuration");
  163. }
  164. - (void)setDbPeakLevelUpdate:(double)intervalInSecs result: (FlutterResult)result {
  165. dbPeakInterval = intervalInSecs;
  166. result(@"setDbPeakLevelUpdate");
  167. }
  168. - (void)setDbLevelEnabled:(BOOL)enabled result: (FlutterResult)result {
  169. shouldProcessDbLevel = enabled == YES;
  170. result(@"setDbLevelEnabled");
  171. }
  172. - (void)startRecorder :(NSString*)path :(NSNumber*)numChannels :(NSNumber*)sampleRate :(NSNumber*)iosQuality :(NSNumber*)bitRate result: (FlutterResult)result {
  173. if ([path class] == [NSNull class]) {
  174. audioFileURL = [NSURL fileURLWithPath:[GetDirectoryOfType_FlutterSound(NSCachesDirectory) stringByAppendingString:@"sound.aac"]];
  175. } else {
  176. audioFileURL = [NSURL fileURLWithPath: [GetDirectoryOfType_FlutterSound(NSCachesDirectory) stringByAppendingString:path]];
  177. }
  178. NSMutableDictionary *audioSettings = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  179. [NSNumber numberWithFloat:[sampleRate doubleValue]],AVSampleRateKey,
  180. [NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey,
  181. [NSNumber numberWithInt: [numChannels intValue]],AVNumberOfChannelsKey,
  182. [NSNumber numberWithInt: [iosQuality intValue]],AVEncoderAudioQualityKey,nil];
  183. // If bitrate is defined, the use it, otherwise use the OS default
  184. if(![bitRate isEqual:[NSNull null]]) {
  185. [audioSettings setValue:[NSNumber numberWithInt: [bitRate intValue]]
  186. forKey:AVEncoderBitRateKey];
  187. }
  188. // Setup audio session
  189. AVAudioSession *session = [AVAudioSession sharedInstance];
  190. [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  191. // set volume default to speaker
  192. UInt32 doChangeDefaultRoute = 1;
  193. AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
  194. // set up for bluetooth microphone input
  195. UInt32 allowBluetoothInput = 1;
  196. AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
  197. audioRecorder = [[AVAudioRecorder alloc]
  198. initWithURL:audioFileURL
  199. settings:audioSettings
  200. error:nil];
  201. [audioRecorder setDelegate:self];
  202. [audioRecorder record];
  203. [self startRecorderTimer];
  204. [audioRecorder setMeteringEnabled:shouldProcessDbLevel];
  205. if(shouldProcessDbLevel == true) {
  206. [self startDbTimer];
  207. }
  208. NSString *filePath = self->audioFileURL.path;
  209. result(filePath);
  210. }
  211. - (void)stopRecorder:(FlutterResult)result {
  212. [audioRecorder stop];
  213. // Stop Db Timer
  214. [dbPeakTimer invalidate];
  215. dbPeakTimer = nil;
  216. [self stopTimer];
  217. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  218. [audioSession setActive:NO error:nil];
  219. NSString *filePath = audioFileURL.absoluteString;
  220. result(filePath);
  221. }
  222. - (void)startPlayer:(NSString*)path result: (FlutterResult)result {
  223. bool isRemote = false;
  224. if ([path class] == [NSNull class]) {
  225. audioFileURL = [NSURL fileURLWithPath:[GetDirectoryOfType_FlutterSound(NSCachesDirectory) stringByAppendingString:@"sound.aac"]];
  226. } else {
  227. NSURL *remoteUrl = [NSURL URLWithString:path];
  228. if(remoteUrl && remoteUrl.scheme && remoteUrl.host){
  229. audioFileURL = remoteUrl;
  230. isRemote = true;
  231. } else {
  232. audioFileURL = [NSURL URLWithString:path];
  233. }
  234. }
  235. if (isRemote) {
  236. NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
  237. dataTaskWithURL:audioFileURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  238. // NSData *data = [NSData dataWithContentsOfURL:audioFileURL];
  239. // We must create a new Audio Player instance to be able to play a different Url
  240. self->audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
  241. self->audioPlayer.delegate = self;
  242. // Able to play in silent mode
  243. [[AVAudioSession sharedInstance]
  244. setCategory: AVAudioSessionCategoryPlayback
  245. error: nil];
  246. // Able to play in background
  247. [[AVAudioSession sharedInstance] setActive: YES error: nil];
  248. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  249. [self->audioPlayer play];
  250. [self startTimer];
  251. NSString *filePath = self->audioFileURL.absoluteString;
  252. result(filePath);
  253. }];
  254. [downloadTask resume];
  255. } else {
  256. // if (!audioPlayer) { // Fix sound distoring when playing recorded audio again.
  257. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
  258. audioPlayer.delegate = self;
  259. // }
  260. // Able to play in silent mode
  261. [[AVAudioSession sharedInstance]
  262. setCategory: AVAudioSessionCategoryPlayback
  263. error: nil];
  264. [audioPlayer play];
  265. [self startTimer];
  266. NSString *filePath = audioFileURL.absoluteString;
  267. result(filePath);
  268. }
  269. }
  270. - (void)stopPlayer:(FlutterResult)result {
  271. if (audioPlayer) {
  272. if (timer != nil) {
  273. [timer invalidate];
  274. timer = nil;
  275. }
  276. [audioPlayer stop];
  277. audioPlayer = nil;
  278. result(@"stop play");
  279. } else {
  280. result([FlutterError
  281. errorWithCode:@"Audio Player"
  282. message:@"player is not set"
  283. details:nil]);
  284. }
  285. }
  286. - (void)pausePlayer:(FlutterResult)result {
  287. if (audioPlayer && [audioPlayer isPlaying]) {
  288. [audioPlayer pause];
  289. if (timer != nil) {
  290. [timer invalidate];
  291. timer = nil;
  292. }
  293. result(@"pause play");
  294. } else {
  295. result([FlutterError
  296. errorWithCode:@"Audio Player"
  297. message:@"player is not set"
  298. details:nil]);
  299. }
  300. }
  301. - (void)resumePlayer:(FlutterResult)result {
  302. if (!audioFileURL) {
  303. result([FlutterError
  304. errorWithCode:@"Audio Player"
  305. message:@"fileURL is not defined"
  306. details:nil]);
  307. return;
  308. }
  309. if (!audioPlayer) {
  310. result([FlutterError
  311. errorWithCode:@"Audio Player"
  312. message:@"player is not set"
  313. details:nil]);
  314. return;
  315. }
  316. [[AVAudioSession sharedInstance]
  317. setCategory: AVAudioSessionCategoryPlayback
  318. error: nil];
  319. [audioPlayer play];
  320. [self startTimer];
  321. NSString *filePath = audioFileURL.absoluteString;
  322. result(filePath);
  323. }
  324. - (void)seekToPlayer:(nonnull NSNumber*) time result: (FlutterResult)result {
  325. if (audioPlayer) {
  326. audioPlayer.currentTime = [time doubleValue] / 1000;
  327. [self updateProgress:nil];
  328. result([time stringValue]);
  329. } else {
  330. result([FlutterError
  331. errorWithCode:@"Audio Player"
  332. message:@"player is not set"
  333. details:nil]);
  334. }
  335. }
  336. - (void)setVolume:(double) volume result: (FlutterResult)result {
  337. if (audioPlayer) {
  338. [audioPlayer setVolume: volume];
  339. result(@"volume set");
  340. } else {
  341. result([FlutterError
  342. errorWithCode:@"Audio Player"
  343. message:@"player is not set"
  344. details:nil]);
  345. }
  346. }
  347. @end