PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ios/CDVSound.m

https://gitlab.com/dannywillems/cordova-plugin-media
Objective C | 891 lines | 659 code | 121 blank | 111 comment | 217 complexity | 6096add84121d39d11ad108682b7fadd MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVSound.h"
  18. #import "CDVFile.h"
  19. #import <AVFoundation/AVFoundation.h>
  20. #define DOCUMENTS_SCHEME_PREFIX @"documents://"
  21. #define HTTP_SCHEME_PREFIX @"http://"
  22. #define HTTPS_SCHEME_PREFIX @"https://"
  23. #define CDVFILE_PREFIX @"cdvfile://"
  24. #define RECORDING_WAV @"wav"
  25. @implementation CDVSound
  26. @synthesize soundCache, avSession, currMediaId;
  27. // Maps a url for a resource path for recording
  28. - (NSURL*)urlForRecording:(NSString*)resourcePath
  29. {
  30. NSURL* resourceURL = nil;
  31. NSString* filePath = nil;
  32. NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  33. // first check for correct extension
  34. if ([[resourcePath pathExtension] caseInsensitiveCompare:RECORDING_WAV] != NSOrderedSame) {
  35. resourceURL = nil;
  36. NSLog(@"Resource for recording must have %@ extension", RECORDING_WAV);
  37. } else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
  38. // try to find Documents:// resources
  39. filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
  40. NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
  41. } else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
  42. CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
  43. CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
  44. filePath = [filePlugin filesystemPathForURL:url];
  45. if (filePath == nil) {
  46. resourceURL = [NSURL URLWithString:resourcePath];
  47. }
  48. } else {
  49. // if resourcePath is not from FileSystem put in tmp dir, else attempt to use provided resource path
  50. NSString* tmpPath = [NSTemporaryDirectory()stringByStandardizingPath];
  51. BOOL isTmp = [resourcePath rangeOfString:tmpPath].location != NSNotFound;
  52. BOOL isDoc = [resourcePath rangeOfString:docsPath].location != NSNotFound;
  53. if (!isTmp && !isDoc) {
  54. // put in temp dir
  55. filePath = [NSString stringWithFormat:@"%@/%@", tmpPath, resourcePath];
  56. } else {
  57. filePath = resourcePath;
  58. }
  59. }
  60. if (filePath != nil) {
  61. // create resourceURL
  62. resourceURL = [NSURL fileURLWithPath:filePath];
  63. }
  64. return resourceURL;
  65. }
  66. // Maps a url for a resource path for playing
  67. // "Naked" resource paths are assumed to be from the www folder as its base
  68. - (NSURL*)urlForPlaying:(NSString*)resourcePath
  69. {
  70. NSURL* resourceURL = nil;
  71. NSString* filePath = nil;
  72. // first try to find HTTP:// or Documents:// resources
  73. if ([resourcePath hasPrefix:HTTP_SCHEME_PREFIX] || [resourcePath hasPrefix:HTTPS_SCHEME_PREFIX]) {
  74. // if it is a http url, use it
  75. NSLog(@"Will use resource '%@' from the Internet.", resourcePath);
  76. resourceURL = [NSURL URLWithString:resourcePath];
  77. } else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
  78. NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  79. filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
  80. NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
  81. } else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
  82. CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
  83. CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
  84. filePath = [filePlugin filesystemPathForURL:url];
  85. if (filePath == nil) {
  86. resourceURL = [NSURL URLWithString:resourcePath];
  87. }
  88. } else {
  89. // attempt to find file path in www directory or LocalFileSystem.TEMPORARY directory
  90. filePath = [self.commandDelegate pathForResource:resourcePath];
  91. if (filePath == nil) {
  92. // see if this exists in the documents/temp directory from a previous recording
  93. NSString* testPath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], resourcePath];
  94. if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) {
  95. // inefficient as existence will be checked again below but only way to determine if file exists from previous recording
  96. filePath = testPath;
  97. NSLog(@"Will attempt to use file resource from LocalFileSystem.TEMPORARY directory");
  98. } else {
  99. // attempt to use path provided
  100. filePath = resourcePath;
  101. NSLog(@"Will attempt to use file resource '%@'", filePath);
  102. }
  103. } else {
  104. NSLog(@"Found resource '%@' in the web folder.", filePath);
  105. }
  106. }
  107. // if the resourcePath resolved to a file path, check that file exists
  108. if (filePath != nil) {
  109. // create resourceURL
  110. resourceURL = [NSURL fileURLWithPath:filePath];
  111. // try to access file
  112. NSFileManager* fMgr = [NSFileManager defaultManager];
  113. if (![fMgr fileExistsAtPath:filePath]) {
  114. resourceURL = nil;
  115. NSLog(@"Unknown resource '%@'", resourcePath);
  116. }
  117. }
  118. return resourceURL;
  119. }
  120. // Creates or gets the cached audio file resource object
  121. - (CDVAudioFile*)audioFileForResource:(NSString*)resourcePath withId:(NSString*)mediaId doValidation:(BOOL)bValidate forRecording:(BOOL)bRecord
  122. {
  123. BOOL bError = NO;
  124. CDVMediaError errcode = MEDIA_ERR_NONE_SUPPORTED;
  125. NSString* errMsg = @"";
  126. NSString* jsString = nil;
  127. CDVAudioFile* audioFile = nil;
  128. NSURL* resourceURL = nil;
  129. if ([self soundCache] == nil) {
  130. [self setSoundCache:[NSMutableDictionary dictionaryWithCapacity:1]];
  131. } else {
  132. audioFile = [[self soundCache] objectForKey:mediaId];
  133. }
  134. if (audioFile == nil) {
  135. // validate resourcePath and create
  136. if ((resourcePath == nil) || ![resourcePath isKindOfClass:[NSString class]] || [resourcePath isEqualToString:@""]) {
  137. bError = YES;
  138. errcode = MEDIA_ERR_ABORTED;
  139. errMsg = @"invalid media src argument";
  140. } else {
  141. audioFile = [[CDVAudioFile alloc] init];
  142. audioFile.resourcePath = resourcePath;
  143. audioFile.resourceURL = nil; // validate resourceURL when actually play or record
  144. [[self soundCache] setObject:audioFile forKey:mediaId];
  145. }
  146. }
  147. if (bValidate && (audioFile.resourceURL == nil)) {
  148. if (bRecord) {
  149. resourceURL = [self urlForRecording:resourcePath];
  150. } else {
  151. resourceURL = [self urlForPlaying:resourcePath];
  152. }
  153. if (resourceURL == nil) {
  154. bError = YES;
  155. errcode = MEDIA_ERR_ABORTED;
  156. errMsg = [NSString stringWithFormat:@"Cannot use audio file from resource '%@'", resourcePath];
  157. } else {
  158. audioFile.resourceURL = resourceURL;
  159. }
  160. }
  161. if (bError) {
  162. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:errcode message:errMsg]];
  163. [self.commandDelegate evalJs:jsString];
  164. }
  165. return audioFile;
  166. }
  167. // returns whether or not audioSession is available - creates it if necessary
  168. - (BOOL)hasAudioSession
  169. {
  170. BOOL bSession = YES;
  171. if (!self.avSession) {
  172. NSError* error = nil;
  173. self.avSession = [AVAudioSession sharedInstance];
  174. if (error) {
  175. // is not fatal if can't get AVAudioSession , just log the error
  176. NSLog(@"error creating audio session: %@", [[error userInfo] description]);
  177. self.avSession = nil;
  178. bSession = NO;
  179. }
  180. }
  181. return bSession;
  182. }
  183. // helper function to create a error object string
  184. - (NSString*)createMediaErrorWithCode:(CDVMediaError)code message:(NSString*)message
  185. {
  186. NSMutableDictionary* errorDict = [NSMutableDictionary dictionaryWithCapacity:2];
  187. [errorDict setObject:[NSNumber numberWithUnsignedInteger:code] forKey:@"code"];
  188. [errorDict setObject:message ? message:@"" forKey:@"message"];
  189. NSData* jsonData = [NSJSONSerialization dataWithJSONObject:errorDict options:0 error:nil];
  190. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  191. }
  192. - (void)create:(CDVInvokedUrlCommand*)command
  193. {
  194. NSString* mediaId = [command argumentAtIndex:0];
  195. NSString* resourcePath = [command argumentAtIndex:1];
  196. CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO];
  197. if (audioFile == nil) {
  198. NSString* errorMessage = [NSString stringWithFormat:@"Failed to initialize Media file with path %@", resourcePath];
  199. NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMessage]];
  200. [self.commandDelegate evalJs:jsString];
  201. } else {
  202. NSURL* resourceUrl = audioFile.resourceURL;
  203. if (![resourceUrl isFileURL] && ![resourcePath hasPrefix:CDVFILE_PREFIX]) {
  204. // First create an AVPlayerItem
  205. AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:resourceUrl];
  206. // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
  207. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
  208. // Subscribe to the AVPlayerItem's PlaybackStalledNotification notification.
  209. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemStalledPlaying:) name:AVPlayerItemPlaybackStalledNotification object:playerItem];
  210. // Pass the AVPlayerItem to a new player
  211. avPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
  212. //avPlayer = [[AVPlayer alloc] initWithURL:resourceUrl];
  213. }
  214. self.currMediaId = mediaId;
  215. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  216. [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
  217. }
  218. }
  219. - (void)setVolume:(CDVInvokedUrlCommand*)command
  220. {
  221. NSString* callbackId = command.callbackId;
  222. #pragma unused(callbackId)
  223. NSString* mediaId = [command argumentAtIndex:0];
  224. NSNumber* volume = [command argumentAtIndex:1 withDefault:[NSNumber numberWithFloat:1.0]];
  225. if ([self soundCache] != nil) {
  226. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  227. if (audioFile != nil) {
  228. audioFile.volume = volume;
  229. if (audioFile.player) {
  230. audioFile.player.volume = [volume floatValue];
  231. }
  232. [[self soundCache] setObject:audioFile forKey:mediaId];
  233. }
  234. }
  235. // don't care for any callbacks
  236. }
  237. - (void)setRate:(CDVInvokedUrlCommand*)command
  238. {
  239. NSString* callbackId = command.callbackId;
  240. #pragma unused(callbackId)
  241. NSString* mediaId = [command argumentAtIndex:0];
  242. NSNumber* rate = [command argumentAtIndex:1 withDefault:[NSNumber numberWithFloat:1.0]];
  243. if ([self soundCache] != nil) {
  244. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  245. if (audioFile != nil) {
  246. audioFile.rate = rate;
  247. if (audioFile.player) {
  248. audioFile.player.enableRate = YES;
  249. audioFile.player.rate = [rate floatValue];
  250. }
  251. if (avPlayer.currentItem && avPlayer.currentItem.asset){
  252. float customRate = [rate floatValue];
  253. [avPlayer setRate:customRate];
  254. }
  255. [[self soundCache] setObject:audioFile forKey:mediaId];
  256. }
  257. }
  258. // don't care for any callbacks
  259. }
  260. - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
  261. {
  262. [self.commandDelegate runInBackground:^{
  263. NSString* callbackId = command.callbackId;
  264. #pragma unused(callbackId)
  265. NSString* mediaId = [command argumentAtIndex:0];
  266. NSString* resourcePath = [command argumentAtIndex:1];
  267. NSDictionary* options = [command argumentAtIndex:2 withDefault:nil];
  268. BOOL bError = NO;
  269. NSString* jsString = nil;
  270. CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO];
  271. if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
  272. if (audioFile.player == nil) {
  273. bError = [self prepareToPlay:audioFile withId:mediaId];
  274. }
  275. if (!bError) {
  276. //self.currMediaId = audioFile.player.mediaId;
  277. self.currMediaId = mediaId;
  278. // audioFile.player != nil or player was successfully created
  279. // get the audioSession and set the category to allow Playing when device is locked or ring/silent switch engaged
  280. if ([self hasAudioSession]) {
  281. NSError* __autoreleasing err = nil;
  282. NSNumber* playAudioWhenScreenIsLocked = [options objectForKey:@"playAudioWhenScreenIsLocked"];
  283. BOOL bPlayAudioWhenScreenIsLocked = YES;
  284. if (playAudioWhenScreenIsLocked != nil) {
  285. bPlayAudioWhenScreenIsLocked = [playAudioWhenScreenIsLocked boolValue];
  286. }
  287. NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryPlayback : AVAudioSessionCategorySoloAmbient;
  288. [self.avSession setCategory:sessionCategory error:&err];
  289. if (![self.avSession setActive:YES error:&err]) {
  290. // other audio with higher priority that does not allow mixing could cause this to fail
  291. NSLog(@"Unable to play audio: %@", [err localizedFailureReason]);
  292. bError = YES;
  293. }
  294. }
  295. if (!bError) {
  296. NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
  297. double position = 0;
  298. if (avPlayer.currentItem && avPlayer.currentItem.asset) {
  299. CMTime time = avPlayer.currentItem.asset.duration;
  300. position = CMTimeGetSeconds(time);
  301. if (audioFile.rate != nil){
  302. float customRate = [audioFile.rate floatValue];
  303. NSLog(@"Playing stream with AVPlayer & custom rate");
  304. [avPlayer setRate:customRate];
  305. } else {
  306. NSLog(@"Playing stream with AVPlayer & custom rate");
  307. [avPlayer play];
  308. }
  309. } else {
  310. NSNumber* loopOption = [options objectForKey:@"numberOfLoops"];
  311. NSInteger numberOfLoops = 0;
  312. if (loopOption != nil) {
  313. numberOfLoops = [loopOption intValue] - 1;
  314. }
  315. audioFile.player.numberOfLoops = numberOfLoops;
  316. if (audioFile.player.isPlaying) {
  317. [audioFile.player stop];
  318. audioFile.player.currentTime = 0;
  319. }
  320. if (audioFile.volume != nil) {
  321. audioFile.player.volume = [audioFile.volume floatValue];
  322. }
  323. audioFile.player.enableRate = YES;
  324. if (audioFile.rate != nil) {
  325. audioFile.player.rate = [audioFile.rate floatValue];
  326. }
  327. [audioFile.player play];
  328. position = round(audioFile.player.duration * 1000) / 1000;
  329. }
  330. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);\n%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_DURATION, position, @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
  331. [self.commandDelegate evalJs:jsString];
  332. }
  333. }
  334. if (bError) {
  335. /* I don't see a problem playing previously recorded audio so removing this section - BG
  336. NSError* error;
  337. // try loading it one more time, in case the file was recorded previously
  338. audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:audioFile.resourceURL error:&error];
  339. if (error != nil) {
  340. NSLog(@"Failed to initialize AVAudioPlayer: %@\n", error);
  341. audioFile.player = nil;
  342. } else {
  343. NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
  344. audioFile.player.numberOfLoops = numberOfLoops;
  345. [audioFile.player play];
  346. } */
  347. // error creating the session or player
  348. // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, MEDIA_ERR_NONE_SUPPORTED];
  349. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
  350. [self.commandDelegate evalJs:jsString];
  351. }
  352. }
  353. // else audioFile was nil - error already returned from audioFile for resource
  354. return;
  355. }];
  356. }
  357. - (BOOL)prepareToPlay:(CDVAudioFile*)audioFile withId:(NSString*)mediaId
  358. {
  359. BOOL bError = NO;
  360. NSError* __autoreleasing playerError = nil;
  361. // create the player
  362. NSURL* resourceURL = audioFile.resourceURL;
  363. if ([resourceURL isFileURL]) {
  364. audioFile.player = [[CDVAudioPlayer alloc] initWithContentsOfURL:resourceURL error:&playerError];
  365. } else {
  366. /*
  367. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:resourceURL];
  368. NSString* userAgent = [self.commandDelegate userAgent];
  369. if (userAgent) {
  370. [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
  371. }
  372. NSURLResponse* __autoreleasing response = nil;
  373. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&playerError];
  374. if (playerError) {
  375. NSLog(@"Unable to download audio from: %@", [resourceURL absoluteString]);
  376. } else {
  377. // bug in AVAudioPlayer when playing downloaded data in NSData - we have to download the file and play from disk
  378. CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
  379. CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
  380. NSString* filePath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], uuidString];
  381. CFRelease(uuidString);
  382. CFRelease(uuidRef);
  383. [data writeToFile:filePath atomically:YES];
  384. NSURL* fileURL = [NSURL fileURLWithPath:filePath];
  385. audioFile.player = [[CDVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&playerError];
  386. }
  387. */
  388. }
  389. if (playerError != nil) {
  390. NSLog(@"Failed to initialize AVAudioPlayer: %@\n", [playerError localizedDescription]);
  391. audioFile.player = nil;
  392. if (self.avSession) {
  393. [self.avSession setActive:NO error:nil];
  394. }
  395. bError = YES;
  396. } else {
  397. audioFile.player.mediaId = mediaId;
  398. audioFile.player.delegate = self;
  399. if (avPlayer == nil)
  400. bError = ![audioFile.player prepareToPlay];
  401. }
  402. return bError;
  403. }
  404. - (void)stopPlayingAudio:(CDVInvokedUrlCommand*)command
  405. {
  406. NSString* mediaId = [command argumentAtIndex:0];
  407. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  408. NSString* jsString = nil;
  409. if ((audioFile != nil) && (audioFile.player != nil)) {
  410. NSLog(@"Stopped playing audio sample '%@'", audioFile.resourcePath);
  411. [audioFile.player stop];
  412. audioFile.player.currentTime = 0;
  413. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  414. }
  415. if (avPlayer.currentItem && avPlayer.currentItem.asset) {
  416. NSLog(@"Stopped playing audio sample '%@'", audioFile.resourcePath);
  417. [avPlayer seekToTime: kCMTimeZero
  418. toleranceBefore: kCMTimeZero
  419. toleranceAfter: kCMTimeZero
  420. completionHandler: ^(BOOL finished){
  421. if (finished) [avPlayer pause];
  422. }];
  423. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  424. }
  425. // ignore if no media playing
  426. if (jsString) {
  427. [self.commandDelegate evalJs:jsString];
  428. }
  429. }
  430. - (void)pausePlayingAudio:(CDVInvokedUrlCommand*)command
  431. {
  432. NSString* mediaId = [command argumentAtIndex:0];
  433. NSString* jsString = nil;
  434. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  435. if ((audioFile != nil) && ((audioFile.player != nil) || (avPlayer != nil))) {
  436. NSLog(@"Paused playing audio sample '%@'", audioFile.resourcePath);
  437. if (audioFile.player != nil) {
  438. [audioFile.player pause];
  439. } else if (avPlayer != nil) {
  440. [avPlayer pause];
  441. }
  442. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_PAUSED];
  443. }
  444. // ignore if no media playing
  445. if (jsString) {
  446. [self.commandDelegate evalJs:jsString];
  447. }
  448. }
  449. - (void)seekToAudio:(CDVInvokedUrlCommand*)command
  450. {
  451. // args:
  452. // 0 = Media id
  453. // 1 = seek to location in milliseconds
  454. NSString* mediaId = [command argumentAtIndex:0];
  455. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  456. double position = [[command argumentAtIndex:1] doubleValue];
  457. double posInSeconds = position / 1000;
  458. NSString* jsString;
  459. if ((audioFile != nil) && (audioFile.player != nil)) {
  460. if (posInSeconds >= audioFile.player.duration) {
  461. // The seek is past the end of file. Stop media and reset to beginning instead of seeking past the end.
  462. [audioFile.player stop];
  463. audioFile.player.currentTime = 0;
  464. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);\n%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_POSITION, 0.0, @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  465. // NSLog(@"seekToEndJsString=%@",jsString);
  466. } else {
  467. audioFile.player.currentTime = posInSeconds;
  468. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%f);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_POSITION, posInSeconds];
  469. // NSLog(@"seekJsString=%@",jsString);
  470. }
  471. } else if (avPlayer != nil) {
  472. int32_t timeScale = avPlayer.currentItem.asset.duration.timescale;
  473. CMTime timeToSeek = CMTimeMakeWithSeconds(posInSeconds, timeScale);
  474. BOOL isPlaying = (avPlayer.rate > 0 && !avPlayer.error);
  475. BOOL isReadyToSeek = (avPlayer.status == AVPlayerStatusReadyToPlay) && (avPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay);
  476. // CB-10535:
  477. // When dealing with remote files, we can get into a situation where we start playing before AVPlayer has had the time to buffer the file to be played.
  478. // To avoid the app crashing in such a situation, we only seek if both the player and the player item are ready to play. If not ready, we send an error back to JS land.
  479. if(isReadyToSeek) {
  480. [avPlayer seekToTime: timeToSeek
  481. toleranceBefore: kCMTimeZero
  482. toleranceAfter: kCMTimeZero
  483. completionHandler: ^(BOOL finished) {
  484. if (isPlaying) [avPlayer play];
  485. }];
  486. } else {
  487. CDVMediaError errcode = MEDIA_ERR_ABORTED;
  488. NSString* errMsg = @"AVPlayerItem cannot service a seek request with a completion handler until its status is AVPlayerItemStatusReadyToPlay.";
  489. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:errcode message:errMsg]];
  490. }
  491. }
  492. [self.commandDelegate evalJs:jsString];
  493. }
  494. - (void)release:(CDVInvokedUrlCommand*)command
  495. {
  496. NSString* mediaId = [command argumentAtIndex:0];
  497. //NSString* mediaId = self.currMediaId;
  498. if (mediaId != nil) {
  499. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  500. if (audioFile != nil) {
  501. if (audioFile.player && [audioFile.player isPlaying]) {
  502. [audioFile.player stop];
  503. }
  504. if (audioFile.recorder && [audioFile.recorder isRecording]) {
  505. [audioFile.recorder stop];
  506. }
  507. if (avPlayer != nil) {
  508. [avPlayer pause];
  509. avPlayer = nil;
  510. }
  511. if (self.avSession) {
  512. [self.avSession setActive:NO error:nil];
  513. self.avSession = nil;
  514. }
  515. [[self soundCache] removeObjectForKey:mediaId];
  516. NSLog(@"Media with id %@ released", mediaId);
  517. }
  518. }
  519. }
  520. - (void)getCurrentPositionAudio:(CDVInvokedUrlCommand*)command
  521. {
  522. NSString* callbackId = command.callbackId;
  523. NSString* mediaId = [command argumentAtIndex:0];
  524. #pragma unused(mediaId)
  525. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  526. double position = -1;
  527. if ((audioFile != nil) && (audioFile.player != nil) && [audioFile.player isPlaying]) {
  528. position = round(audioFile.player.currentTime * 1000) / 1000;
  529. }
  530. if (avPlayer) {
  531. CMTime time = [avPlayer currentTime];
  532. position = CMTimeGetSeconds(time);
  533. }
  534. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:position];
  535. NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_POSITION, position];
  536. [self.commandDelegate evalJs:jsString];
  537. [self.commandDelegate sendPluginResult:result callbackId:callbackId];
  538. }
  539. - (void)startRecordingAudio:(CDVInvokedUrlCommand*)command
  540. {
  541. NSString* callbackId = command.callbackId;
  542. #pragma unused(callbackId)
  543. NSString* mediaId = [command argumentAtIndex:0];
  544. CDVAudioFile* audioFile = [self audioFileForResource:[command argumentAtIndex:1] withId:mediaId doValidation:YES forRecording:YES];
  545. __block NSString* jsString = nil;
  546. __block NSString* errorMsg = @"";
  547. if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
  548. __weak CDVSound* weakSelf = self;
  549. void (^startRecording)(void) = ^{
  550. NSError* __autoreleasing error = nil;
  551. if (audioFile.recorder != nil) {
  552. [audioFile.recorder stop];
  553. audioFile.recorder = nil;
  554. }
  555. // get the audioSession and set the category to allow recording when device is locked or ring/silent switch engaged
  556. if ([weakSelf hasAudioSession]) {
  557. if (![weakSelf.avSession.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
  558. [weakSelf.avSession setCategory:AVAudioSessionCategoryRecord error:nil];
  559. }
  560. if (![weakSelf.avSession setActive:YES error:&error]) {
  561. // other audio with higher priority that does not allow mixing could cause this to fail
  562. errorMsg = [NSString stringWithFormat:@"Unable to record audio: %@", [error localizedFailureReason]];
  563. // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, MEDIA_ERR_ABORTED];
  564. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [weakSelf createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
  565. [weakSelf.commandDelegate evalJs:jsString];
  566. return;
  567. }
  568. }
  569. // create a new recorder for each start record
  570. NSDictionary *audioSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
  571. AVSampleRateKey: @(44100),
  572. AVNumberOfChannelsKey: @(1),
  573. AVEncoderAudioQualityKey: @(AVAudioQualityMedium)
  574. };
  575. audioFile.recorder = [[CDVAudioRecorder alloc] initWithURL:audioFile.resourceURL settings:nil error:&error];
  576. bool recordingSuccess = NO;
  577. if (error == nil) {
  578. audioFile.recorder.delegate = weakSelf;
  579. audioFile.recorder.mediaId = mediaId;
  580. audioFile.recorder.meteringEnabled = YES;
  581. recordingSuccess = [audioFile.recorder record];
  582. if (recordingSuccess) {
  583. NSLog(@"Started recording audio sample '%@'", audioFile.resourcePath);
  584. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
  585. [weakSelf.commandDelegate evalJs:jsString];
  586. }
  587. }
  588. if ((error != nil) || (recordingSuccess == NO)) {
  589. if (error != nil) {
  590. errorMsg = [NSString stringWithFormat:@"Failed to initialize AVAudioRecorder: %@\n", [error localizedFailureReason]];
  591. } else {
  592. errorMsg = @"Failed to start recording using AVAudioRecorder";
  593. }
  594. audioFile.recorder = nil;
  595. if (weakSelf.avSession) {
  596. [weakSelf.avSession setActive:NO error:nil];
  597. }
  598. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [weakSelf createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
  599. [weakSelf.commandDelegate evalJs:jsString];
  600. }
  601. };
  602. SEL rrpSel = NSSelectorFromString(@"requestRecordPermission:");
  603. if ([self hasAudioSession] && [self.avSession respondsToSelector:rrpSel])
  604. {
  605. #pragma clang diagnostic push
  606. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  607. [self.avSession performSelector:rrpSel withObject:^(BOOL granted){
  608. if (granted) {
  609. startRecording();
  610. } else {
  611. NSString* msg = @"Error creating audio session, microphone permission denied.";
  612. NSLog(@"%@", msg);
  613. audioFile.recorder = nil;
  614. if (weakSelf.avSession) {
  615. [weakSelf.avSession setActive:NO error:nil];
  616. }
  617. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:msg]];
  618. [weakSelf.commandDelegate evalJs:jsString];
  619. }
  620. }];
  621. #pragma clang diagnostic pop
  622. } else {
  623. startRecording();
  624. }
  625. } else {
  626. // file did not validate
  627. NSString* errorMsg = [NSString stringWithFormat:@"Could not record audio at '%@'", audioFile.resourcePath];
  628. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
  629. [self.commandDelegate evalJs:jsString];
  630. }
  631. }
  632. - (void)stopRecordingAudio:(CDVInvokedUrlCommand*)command
  633. {
  634. NSString* mediaId = [command argumentAtIndex:0];
  635. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  636. NSString* jsString = nil;
  637. if ((audioFile != nil) && (audioFile.recorder != nil)) {
  638. NSLog(@"Stopped recording audio sample '%@'", audioFile.resourcePath);
  639. [audioFile.recorder stop];
  640. // no callback - that will happen in audioRecorderDidFinishRecording
  641. }
  642. // ignore if no media recording
  643. if (jsString) {
  644. [self.commandDelegate evalJs:jsString];
  645. }
  646. }
  647. - (void)audioRecorderDidFinishRecording:(AVAudioRecorder*)recorder successfully:(BOOL)flag
  648. {
  649. CDVAudioRecorder* aRecorder = (CDVAudioRecorder*)recorder;
  650. NSString* mediaId = aRecorder.mediaId;
  651. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  652. NSString* jsString = nil;
  653. if (audioFile != nil) {
  654. NSLog(@"Finished recording audio sample '%@'", audioFile.resourcePath);
  655. }
  656. if (flag) {
  657. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  658. } else {
  659. // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, MEDIA_ERR_DECODE];
  660. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
  661. }
  662. if (self.avSession) {
  663. [self.avSession setActive:NO error:nil];
  664. }
  665. [self.commandDelegate evalJs:jsString];
  666. }
  667. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag
  668. {
  669. //commented as unused
  670. CDVAudioPlayer* aPlayer = (CDVAudioPlayer*)player;
  671. NSString* mediaId = aPlayer.mediaId;
  672. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  673. NSString* jsString = nil;
  674. if (audioFile != nil) {
  675. NSLog(@"Finished playing audio sample '%@'", audioFile.resourcePath);
  676. }
  677. if (flag) {
  678. audioFile.player.currentTime = 0;
  679. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  680. } else {
  681. // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, MEDIA_ERR_DECODE];
  682. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
  683. }
  684. if (self.avSession) {
  685. [self.avSession setActive:NO error:nil];
  686. }
  687. [self.commandDelegate evalJs:jsString];
  688. }
  689. -(void)itemDidFinishPlaying:(NSNotification *) notification {
  690. // Will be called when AVPlayer finishes playing playerItem
  691. NSString* mediaId = self.currMediaId;
  692. NSString* jsString = nil;
  693. jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
  694. if (self.avSession) {
  695. [self.avSession setActive:NO error:nil];
  696. }
  697. [self.commandDelegate evalJs:jsString];
  698. }
  699. -(void)itemStalledPlaying:(NSNotification *) notification {
  700. // Will be called when playback stalls due to buffer empty
  701. NSLog(@"Stalled playback");
  702. }
  703. - (void)onMemoryWarning
  704. {
  705. [[self soundCache] removeAllObjects];
  706. [self setSoundCache:nil];
  707. [self setAvSession:nil];
  708. [super onMemoryWarning];
  709. }
  710. - (void)dealloc
  711. {
  712. [[self soundCache] removeAllObjects];
  713. }
  714. - (void)onReset
  715. {
  716. for (CDVAudioFile* audioFile in [[self soundCache] allValues]) {
  717. if (audioFile != nil) {
  718. if (audioFile.player != nil) {
  719. [audioFile.player stop];
  720. audioFile.player.currentTime = 0;
  721. }
  722. if (audioFile.recorder != nil) {
  723. [audioFile.recorder stop];
  724. }
  725. }
  726. }
  727. [[self soundCache] removeAllObjects];
  728. }
  729. - (void)getCurrentAmplitudeAudio:(CDVInvokedUrlCommand*)command
  730. {
  731. NSString* callbackId = command.callbackId;
  732. NSString* mediaId = [command argumentAtIndex:0];
  733. #pragma unused(mediaId)
  734. CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
  735. float amplitude = 0; // The linear 0.0 .. 1.0 value
  736. if ((audioFile != nil) && (audioFile.recorder != nil) && [audioFile.recorder isRecording]) {
  737. [audioFile.recorder updateMeters];
  738. float minDecibels = -60.0f; // Or use -60dB, which I measured in a silent room.
  739. float decibels = [audioFile.recorder averagePowerForChannel:0];
  740. if (decibels < minDecibels) {
  741. amplitude = 0.0f;
  742. } else if (decibels >= 0.0f) {
  743. amplitude = 1.0f;
  744. } else {
  745. float root = 2.0f;
  746. float minAmp = powf(10.0f, 0.05f * minDecibels);
  747. float inverseAmpRange = 1.0f / (1.0f - minAmp);
  748. float amp = powf(10.0f, 0.05f * decibels);
  749. float adjAmp = (amp - minAmp) * inverseAmpRange;
  750. amplitude = powf(adjAmp, 1.0f / root);
  751. }
  752. }
  753. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:amplitude];
  754. NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_POSITION, amplitude];
  755. [self.commandDelegate evalJs:jsString];
  756. [self.commandDelegate sendPluginResult:result callbackId:callbackId];
  757. }
  758. @end
  759. @implementation CDVAudioFile
  760. @synthesize resourcePath;
  761. @synthesize resourceURL;
  762. @synthesize player, volume, rate;
  763. @synthesize recorder;
  764. @end
  765. @implementation CDVAudioPlayer
  766. @synthesize mediaId;
  767. @end
  768. @implementation CDVAudioRecorder
  769. @synthesize mediaId;
  770. @end