FlutterSoundPlugin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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: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. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
  189. // Setup audio session
  190. AVAudioSession *session = [AVAudioSession sharedInstance];
  191. [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  192. // set volume default to speaker
  193. UInt32 doChangeDefaultRoute = 1;
  194. AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
  195. // set up for bluetooth microphone input
  196. UInt32 allowBluetoothInput = 1;
  197. AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
  198. audioRecorder = [[AVAudioRecorder alloc]
  199. initWithURL:audioFileURL
  200. settings:audioSettings
  201. error:nil];
  202. [audioRecorder setDelegate:self];
  203. if([audioRecorder prepareToRecord]) {
  204. [audioRecorder record];
  205. [self startRecorderTimer];
  206. [audioRecorder setMeteringEnabled:shouldProcessDbLevel];
  207. }
  208. if(shouldProcessDbLevel == true) {
  209. [self startDbTimer];
  210. }
  211. NSString *filePath = self->audioFileURL.path;
  212. result(filePath);
  213. }
  214. - (void)stopRecorder:(FlutterResult)result {
  215. [audioRecorder stop];
  216. // Stop Db Timer
  217. [dbPeakTimer invalidate];
  218. dbPeakTimer = nil;
  219. [self stopTimer];
  220. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  221. [audioSession setActive:NO error:nil];
  222. NSString *filePath = audioFileURL.absoluteString;
  223. result(filePath);
  224. }
  225. - (void)startPlayer:(NSString*)path result: (FlutterResult)result {
  226. bool isRemote = false;
  227. if ([path class] == [NSNull class]) {
  228. audioFileURL = [NSURL fileURLWithPath:[GetDirectoryOfType_FlutterSound(NSCachesDirectory) stringByAppendingString:@"sound.aac"]];
  229. } else {
  230. NSURL *remoteUrl = [NSURL URLWithString:path];
  231. if(remoteUrl && remoteUrl.scheme && remoteUrl.host){
  232. audioFileURL = remoteUrl;
  233. isRemote = true;
  234. } else {
  235. audioFileURL = [NSURL URLWithString:path];
  236. }
  237. }
  238. if (isRemote) {
  239. NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
  240. dataTaskWithURL:audioFileURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  241. // NSData *data = [NSData dataWithContentsOfURL:audioFileURL];
  242. // We must create a new Audio Player instance to be able to play a different Url
  243. self->audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
  244. self->audioPlayer.delegate = self;
  245. // Able to play in silent mode
  246. [[AVAudioSession sharedInstance]
  247. setCategory: AVAudioSessionCategoryPlayback
  248. error: nil];
  249. // Able to play in background
  250. [[AVAudioSession sharedInstance] setActive: YES error: nil];
  251. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  252. [self->audioPlayer play];
  253. [self startTimer];
  254. NSString *filePath = self->audioFileURL.absoluteString;
  255. result(filePath);
  256. }];
  257. [downloadTask resume];
  258. } else {
  259. NSData *imageData = [NSData dataWithContentsOfFile:path];
  260. // if (!audioPlayer) { // Fix sound distoring when playing recorded audio again.
  261. // audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
  262. // audioPlayer.delegate = self;
  263. // }
  264. self->audioPlayer = [[AVAudioPlayer alloc] initWithData:imageData error:nil];
  265. self->audioPlayer.delegate = self;
  266. // Able to play in silent mode
  267. [[AVAudioSession sharedInstance]
  268. setCategory: AVAudioSessionCategoryPlayback
  269. error: nil];
  270. [audioPlayer play];
  271. [self startTimer];
  272. NSString *filePath = audioFileURL.absoluteString;
  273. result(filePath);
  274. }
  275. }
  276. - (void)stopPlayer:(FlutterResult)result {
  277. if (audioPlayer) {
  278. if (timer != nil) {
  279. [timer invalidate];
  280. timer = nil;
  281. }
  282. [audioPlayer stop];
  283. audioPlayer = nil;
  284. result(@"stop play");
  285. } else {
  286. result([FlutterError
  287. errorWithCode:@"Audio Player"
  288. message:@"player is not set"
  289. details:nil]);
  290. }
  291. }
  292. - (void)pausePlayer:(FlutterResult)result {
  293. if (audioPlayer && [audioPlayer isPlaying]) {
  294. [audioPlayer pause];
  295. if (timer != nil) {
  296. [timer invalidate];
  297. timer = nil;
  298. }
  299. result(@"pause play");
  300. } else {
  301. result([FlutterError
  302. errorWithCode:@"Audio Player"
  303. message:@"player is not set"
  304. details:nil]);
  305. }
  306. }
  307. - (void)resumePlayer:(FlutterResult)result {
  308. if (!audioFileURL) {
  309. result([FlutterError
  310. errorWithCode:@"Audio Player"
  311. message:@"fileURL is not defined"
  312. details:nil]);
  313. return;
  314. }
  315. if (!audioPlayer) {
  316. result([FlutterError
  317. errorWithCode:@"Audio Player"
  318. message:@"player is not set"
  319. details:nil]);
  320. return;
  321. }
  322. [[AVAudioSession sharedInstance]
  323. setCategory: AVAudioSessionCategoryPlayback
  324. error: nil];
  325. [audioPlayer play];
  326. [self startTimer];
  327. NSString *filePath = audioFileURL.absoluteString;
  328. result(filePath);
  329. }
  330. - (void)seekToPlayer:(nonnull NSNumber*) time result: (FlutterResult)result {
  331. if (audioPlayer) {
  332. audioPlayer.currentTime = [time doubleValue] / 1000;
  333. [self updateProgress:nil];
  334. result([time stringValue]);
  335. } else {
  336. result([FlutterError
  337. errorWithCode:@"Audio Player"
  338. message:@"player is not set"
  339. details:nil]);
  340. }
  341. }
  342. - (void)setVolume:(double) volume result: (FlutterResult)result {
  343. if (audioPlayer) {
  344. [audioPlayer setVolume: volume];
  345. result(@"volume set");
  346. } else {
  347. result([FlutterError
  348. errorWithCode:@"Audio Player"
  349. message:@"player is not set"
  350. details:nil]);
  351. }
  352. }
  353. @end