Advertisement
Guest User

cdv update

a guest
Jan 26th, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
  2. {
  3.     NSString* callbackId = command.callbackId;
  4.  
  5. #pragma unused(callbackId)
  6.     NSString* mediaId = [command.arguments objectAtIndex:0];
  7.     NSString* resourcePath = [command.arguments objectAtIndex:1];
  8.     NSDictionary* options = [command.arguments objectAtIndex:2 withDefault:nil];
  9.  
  10.     BOOL bError = NO;
  11.     NSString* jsString = nil;
  12.  
  13.     CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO];
  14.     if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
  15.  
  16.         if (!bError) {
  17.             // audioFile.player != nil  or player was successfully created
  18.             // get the audioSession and set the category to allow Playing when device is locked or ring/silent switch engaged
  19.             if ([self hasAudioSession]) {
  20.                 NSError* __autoreleasing err = nil;
  21.                 NSNumber* playAudioWhenScreenIsLocked = [options objectForKey:@"playAudioWhenScreenIsLocked"];
  22.                 BOOL bPlayAudioWhenScreenIsLocked = YES;
  23.                 if (playAudioWhenScreenIsLocked != nil) {
  24.                     bPlayAudioWhenScreenIsLocked = [playAudioWhenScreenIsLocked boolValue];
  25.                 }
  26.  
  27.                 NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryAmbient : AVAudioSessionCategoryAmbient;
  28.                 [self.avSession setCategory:sessionCategory error:&err];
  29.                
  30.                
  31.                 if (audioFile.player == nil) {
  32.                     bError = [self prepareToPlay:audioFile withId:mediaId];
  33.                 }
  34.                
  35.                 if (![self.avSession setActive:YES error:&err]) {
  36.                     // other audio with higher priority that does not allow mixing could cause this to fail
  37.                     NSLog(@"Unable to play audio: %@", [err localizedFailureReason]);
  38.                     bError = YES;
  39.                 }
  40.             }
  41.             if (!bError) {
  42.                 NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
  43.                 NSNumber* loopOption = [options objectForKey:@"numberOfLoops"];
  44.                 NSInteger numberOfLoops = 0;
  45.                 if (loopOption != nil) {
  46.                     numberOfLoops = [loopOption intValue] - 1;
  47.                 }
  48.                 audioFile.player.numberOfLoops = numberOfLoops;
  49.                 if (audioFile.player.isPlaying) {
  50.                     [audioFile.player stop];
  51.                     audioFile.player.currentTime = 0;
  52.                 }
  53.                 if (audioFile.volume != nil) {
  54.                     audioFile.player.volume = [audioFile.volume floatValue];
  55.                 }
  56.                
  57.                 if (audioFile.player == nil) {
  58.                     bError = [self prepareToPlay:audioFile withId:mediaId];
  59.                 }
  60.  
  61.                 [audioFile.player play];
  62.                 double position = round(audioFile.player.duration * 1000) / 1000;
  63.                 jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);\n%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_DURATION, position, @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
  64.                 [self.commandDelegate evalJs:jsString];
  65.             }
  66.         }
  67.         if (bError) {
  68.             /*  I don't see a problem playing previously recorded audio so removing this section - BG
  69.             NSError* error;
  70.             // try loading it one more time, in case the file was recorded previously
  71.             audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:audioFile.resourceURL error:&error];
  72.             if (error != nil) {
  73.                 NSLog(@"Failed to initialize AVAudioPlayer: %@\n", error);
  74.                 audioFile.player = nil;
  75.             } else {
  76.                 NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
  77.                 audioFile.player.numberOfLoops = numberOfLoops;
  78.                 [audioFile.player play];
  79.             } */
  80.             // error creating the session or player
  81.             // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR,  MEDIA_ERR_NONE_SUPPORTED];
  82.             jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
  83.             [self.commandDelegate evalJs:jsString];
  84.         }
  85.     }
  86.     // else audioFile was nil - error already returned from audioFile for resource
  87.     return;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement